merge with master
[platform/framework/web/wrt-commons.git] / modules / log / src / old_style_log_provider.cpp
1 /*
2  * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  *    Licensed under the Apache License, Version 2.0 (the "License");
5  *    you may not use this file except in compliance with the License.
6  *    You may obtain a copy of the License at
7  *
8  *        http://www.apache.org/licenses/LICENSE-2.0
9  *
10  *    Unless required by applicable law or agreed to in writing, software
11  *    distributed under the License is distributed on an "AS IS" BASIS,
12  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *    See the License for the specific language governing permissions and
14  *    limitations under the License.
15  */
16 /*
17  * @file        old_style_log_provider.cpp
18  * @author      Przemyslaw Dobrowolski (p.dobrowolsk@samsung.com)
19  * @version     1.0
20  * @brief       This file is the implementation file of old style log provider
21  */
22 #include <stddef.h>
23 #include <dpl/log/old_style_log_provider.h>
24 #include <dpl/colors.h>
25 #include <cstdio>
26 #include <cstring>
27 #include <sstream>
28 #include <sys/time.h>
29 #include <unistd.h>
30
31 namespace DPL {
32 namespace Log {
33 namespace // anonymous
34 {
35 using namespace DPL::Colors::Text;
36 const char *DEBUG_BEGIN = GREEN_BEGIN;
37 const char *DEBUG_END = GREEN_END;
38 const char *INFO_BEGIN = CYAN_BEGIN;
39 const char *INFO_END = CYAN_END;
40 const char *ERROR_BEGIN = RED_BEGIN;
41 const char *ERROR_END = RED_END;
42 const char *WARNING_BEGIN = BOLD_GOLD_BEGIN;
43 const char *WARNING_END = BOLD_GOLD_END;
44 const char *PEDANTIC_BEGIN = PURPLE_BEGIN;
45 const char *PEDANTIC_END = PURPLE_END;
46
47 std::string GetFormattedTime()
48 {
49     timeval tv;
50     tm localNowTime;
51
52     gettimeofday(&tv, NULL);
53     localtime_r(&tv.tv_sec, &localNowTime);
54
55     char format[64];
56     snprintf(format,
57              sizeof(format),
58              "%02i:%02i:%02i.%03i",
59              localNowTime.tm_hour,
60              localNowTime.tm_min,
61              localNowTime.tm_sec,
62              static_cast<int>(tv.tv_usec / 1000));
63     return format;
64 }
65 } // namespace anonymous
66
67 std::string OldStyleLogProvider::FormatMessage(const char *message,
68                                                const char *filename,
69                                                int line,
70                                                const char *function)
71 {
72     std::ostringstream val;
73
74     val << std::string("[") << GetFormattedTime() << std::string("] [") <<
75     static_cast<unsigned long>(pthread_self()) << "/" <<
76     static_cast<int>(getpid()) << std::string("] [") <<
77     LocateSourceFileName(filename) << std::string(":") << line <<
78     std::string("] ") << function << std::string("(): ") << message;
79
80     return val.str();
81 }
82
83 OldStyleLogProvider::OldStyleLogProvider(bool showDebug,
84                                          bool showInfo,
85                                          bool showWarning,
86                                          bool showError,
87                                          bool showPedantic) :
88     m_showDebug(showDebug),
89     m_showInfo(showInfo),
90     m_showWarning(showWarning),
91     m_showError(showError),
92     m_showPedantic(showPedantic)
93 {}
94
95 void OldStyleLogProvider::Debug(const char *message,
96                                 const char *filename,
97                                 int line,
98                                 const char *function)
99 {
100     if (m_showDebug) {
101         fprintf(stdout, "%s%s%s\n", DEBUG_BEGIN,
102                 FormatMessage(message, filename, line,
103                               function).c_str(), DEBUG_END);
104     }
105 }
106
107 void OldStyleLogProvider::Info(const char *message,
108                                const char *filename,
109                                int line,
110                                const char *function)
111 {
112     if (m_showInfo) {
113         fprintf(stdout, "%s%s%s\n", INFO_BEGIN,
114                 FormatMessage(message, filename, line,
115                               function).c_str(), INFO_END);
116     }
117 }
118
119 void OldStyleLogProvider::Warning(const char *message,
120                                   const char *filename,
121                                   int line,
122                                   const char *function)
123 {
124     if (m_showWarning) {
125         fprintf(stdout, "%s%s%s\n", WARNING_BEGIN,
126                 FormatMessage(message, filename, line,
127                               function).c_str(), WARNING_END);
128     }
129 }
130
131 void OldStyleLogProvider::Error(const char *message,
132                                 const char *filename,
133                                 int line,
134                                 const char *function)
135 {
136     if (m_showError) {
137         fprintf(stdout, "%s%s%s\n", ERROR_BEGIN,
138                 FormatMessage(message, filename, line,
139                               function).c_str(), ERROR_END);
140     }
141 }
142
143 void OldStyleLogProvider::Pedantic(const char *message,
144                                    const char *filename,
145                                    int line,
146                                    const char *function)
147 {
148     if (m_showPedantic) {
149         fprintf(stdout, "%s%s%s\n", PEDANTIC_BEGIN,
150                 FormatMessage(message, filename, line,
151                               function).c_str(), PEDANTIC_END);
152     }
153 }
154 }
155 } // namespace DPL