[M120 Migration][MM] Handle live stream duration and currenttime
[platform/framework/web/chromium-efl.git] / tizen_src / downloadable / dlog_util.h
1 // Copyright 2019 Samsung Electronics. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #ifndef DLOG_UTIL_H
6 #define DLOG_UTIL_H
7
8 #include <sstream>
9
10 #ifndef LOG_TAG
11 #define LOG_TAG "CHROMIUM"
12 #endif
13 #include <dlog/dlog.h>
14
15 #ifdef FILE_LOG_OUT
16 #include <sys/time.h>
17 #include <unistd.h>
18 #include <fstream>
19 #endif
20
21 #define __DLOG_UTIL_FMT__ "%s:%s(%d) > "
22 #define __DLOG_UTIL_FILE__                                                 \
23   (__builtin_strrchr(__FILE__, '/') ? __builtin_strrchr(__FILE__, '/') + 1 \
24                                     : __FILE__)
25
26 #define TIZEN_DLOG_PRINT(priority, fmt, ...)                               \
27   dlog_print(priority, LOG_TAG, __DLOG_UTIL_FMT__ fmt, __DLOG_UTIL_FILE__, \
28              __FUNCTION__, __LINE__, ##__VA_ARGS__)
29
30 class TizenDlogWrapper {
31  public:
32   TizenDlogWrapper(log_priority priority,
33                    const char* file,
34                    unsigned int line,
35                    const char* func)
36       : priority_(priority), file_(file), line_(line), func_(func) {}
37   ~TizenDlogWrapper() {
38     dlog_print(priority_, LOG_TAG, __DLOG_UTIL_FMT__ "%s", file_, func_, line_,
39                stream_.str().data());
40 #ifdef FILE_LOG_OUT
41     if (!fstream_.is_open())
42       return;
43     struct timespec ts;
44     struct tm local;
45     if (!clock_gettime(CLOCK_MONOTONIC, &ts) &&
46         localtime_r(&ts.tv_sec, &local)) {
47       char buffer[20];
48       int ms = ts.tv_nsec / 1000000;
49       strftime(buffer, sizeof(buffer), "%m-%d %H:%M:%S", &local);
50       fstream_ << buffer;
51       snprintf(buffer, sizeof(buffer), ":%03d ", ms);
52       fstream_ << buffer;
53     }
54     fstream_ << "[" << getpid() << "] " << file_ << ":" << func_ << "(" << line_
55              << ") > " << stream_.str() << std::endl;
56 #endif
57   }
58   std::ostream& stream() { return stream_; }
59
60 #ifdef FILE_LOG_OUT
61   static std::ofstream fstream_;
62 #endif
63
64  private:
65   log_priority priority_;
66   const char* file_;
67   unsigned int line_;
68   const char* func_;
69   std::ostringstream stream_;
70 };
71
72 #define TIZEN_DLOG_STREAM(priority)                                      \
73   TizenDlogWrapper(priority, __DLOG_UTIL_FILE__, __LINE__, __FUNCTION__) \
74       .stream()
75
76 // redefine LOG macro function defined in dlog-internal.h
77 #ifdef LOG
78 #undef LOG
79 #endif
80 #define LOG(priority) TIZEN_DLOG_STREAM(DLOG_##priority)
81
82 #endif  // DLOG_UTIL_H