tizen 2.3 release
[framework/web/wearable/wrt-security.git] / commons / 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     m_printStdErr(false)
94 {}
95
96 OldStyleLogProvider::OldStyleLogProvider(bool showDebug,
97                                          bool showInfo,
98                                          bool showWarning,
99                                          bool showError,
100                                          bool showPedantic,
101                                          bool printStdErr) :
102     m_showDebug(showDebug),
103     m_showInfo(showInfo),
104     m_showWarning(showWarning),
105     m_showError(showError),
106     m_showPedantic(showPedantic),
107     m_printStdErr(printStdErr)
108 {}
109
110 void OldStyleLogProvider::Debug(const char *message,
111                                 const char *filename,
112                                 int line,
113                                 const char *function)
114 {
115     if (m_showDebug) {
116         if (m_printStdErr) {
117             fprintf(stderr, "%s%s%s\n", DEBUG_BEGIN,
118                     FormatMessage(message, filename, line,
119                         function).c_str(), DEBUG_END);
120         } else {
121             fprintf(stdout, "%s%s%s\n", DEBUG_BEGIN,
122                     FormatMessage(message, filename, line,
123                         function).c_str(), DEBUG_END);
124         }
125     }
126 }
127
128 void OldStyleLogProvider::Info(const char *message,
129                                const char *filename,
130                                int line,
131                                const char *function)
132 {
133     if (m_showInfo) {
134         if (m_printStdErr) {
135             fprintf(stderr, "%s%s%s\n", INFO_BEGIN,
136                     FormatMessage(message, filename, line,
137                         function).c_str(), INFO_END);
138         } else {
139             fprintf(stdout, "%s%s%s\n", INFO_BEGIN,
140                     FormatMessage(message, filename, line,
141                         function).c_str(), INFO_END);
142         }
143     }
144 }
145
146 void OldStyleLogProvider::Warning(const char *message,
147                                   const char *filename,
148                                   int line,
149                                   const char *function)
150 {
151     if (m_showWarning) {
152         if (m_printStdErr) {
153             fprintf(stderr, "%s%s%s\n", WARNING_BEGIN,
154                     FormatMessage(message, filename, line,
155                         function).c_str(), WARNING_END);
156         } else {
157             fprintf(stdout, "%s%s%s\n", WARNING_BEGIN,
158                     FormatMessage(message, filename, line,
159                         function).c_str(), WARNING_END);
160         }
161     }
162 }
163
164 void OldStyleLogProvider::Error(const char *message,
165                                 const char *filename,
166                                 int line,
167                                 const char *function)
168 {
169     if (m_showError) {
170         if (m_printStdErr) {
171             fprintf(stderr, "%s%s%s\n", ERROR_BEGIN,
172                     FormatMessage(message, filename, line,
173                         function).c_str(), ERROR_END);
174         } else {
175             fprintf(stdout, "%s%s%s\n", ERROR_BEGIN,
176                     FormatMessage(message, filename, line,
177                         function).c_str(), ERROR_END);
178         }
179     }
180 }
181
182 void OldStyleLogProvider::Pedantic(const char *message,
183                                    const char *filename,
184                                    int line,
185                                    const char *function)
186 {
187     if (m_showPedantic) {
188         if (m_printStdErr) {
189             fprintf(stderr, "%s%s%s\n", PEDANTIC_BEGIN,
190                     FormatMessage(message, filename, line,
191                         function).c_str(), PEDANTIC_END);
192         } else {
193             fprintf(stdout, "%s%s%s\n", PEDANTIC_BEGIN,
194                     FormatMessage(message, filename, line,
195                         function).c_str(), PEDANTIC_END);
196         }
197     }
198 }
199 }
200 } // namespace DPL