tizen beta release
[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 <dpl/log/old_style_log_provider.h>
23 #include <dpl/colors.h>
24 #include <cstdio>
25 #include <cstring>
26 #include <sstream>
27 #include <sys/time.h>
28
29 namespace DPL
30 {
31 namespace Log
32 {
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
48 std::string GetFormattedTime()
49 {
50     timeval tv;
51     tm localNowTime;
52
53     gettimeofday(&tv, NULL);
54     localtime_r(&tv.tv_sec, &localNowTime);
55
56     char format[64];
57     snprintf(format, sizeof(format), "%02i:%02i:%02i.%03i", localNowTime.tm_hour, localNowTime.tm_min, localNowTime.tm_sec, static_cast<int>(tv.tv_usec / 1000));
58     return format;
59 }
60
61 } // namespace anonymous
62
63 std::string OldStyleLogProvider::FormatMessage(const char *message, const char *filename, int line, const char *function)
64 {
65     std::ostringstream val;
66
67     val << std::string("[") << GetFormattedTime() << std::string("] [") <<
68            static_cast<unsigned long>(pthread_self()) << "/" << static_cast<int>(getpid()) << std::string("] [") <<
69            LocateSourceFileName(filename) << std::string(":") << line <<
70            std::string("] ") << function << std::string("(): ") << message;
71
72     return val.str();
73 }
74
75 OldStyleLogProvider::OldStyleLogProvider(bool showDebug, bool showInfo, bool showWarning, bool showError, bool showPedantic)
76     : m_showDebug(showDebug),
77       m_showInfo(showInfo),
78       m_showWarning(showWarning),
79       m_showError(showError),
80       m_showPedantic(showPedantic)
81 {
82 }
83
84 void OldStyleLogProvider::Debug(const char *message, const char *filename, int line, const char *function)
85 {
86     if (m_showDebug)
87         fprintf(stdout, "%s%s%s\n", DEBUG_BEGIN, FormatMessage(message, filename, line, function).c_str(), DEBUG_END);
88 }
89
90 void OldStyleLogProvider::Info(const char *message, const char *filename, int line, const char *function)
91 {
92     if (m_showInfo)
93         fprintf(stdout, "%s%s%s\n", INFO_BEGIN, FormatMessage(message, filename, line, function).c_str(), INFO_END);
94 }
95
96 void OldStyleLogProvider::Warning(const char *message, const char *filename, int line, const char *function)
97 {
98     if (m_showWarning)
99         fprintf(stdout, "%s%s%s\n", WARNING_BEGIN, FormatMessage(message, filename, line, function).c_str(), WARNING_END);
100 }
101
102 void OldStyleLogProvider::Error(const char *message, const char *filename, int line, const char *function)
103 {
104     if (m_showError)
105         fprintf(stdout, "%s%s%s\n", ERROR_BEGIN, FormatMessage(message, filename, line, function).c_str(), ERROR_END);
106 }
107
108 void OldStyleLogProvider::Pedantic(const char *message, const char *filename, int line, const char *function)
109 {
110     if (m_showPedantic)
111         fprintf(stdout, "%s%s%s\n", PEDANTIC_BEGIN, FormatMessage(message, filename, line, function).c_str(), PEDANTIC_END);
112 }
113
114 }
115 } // namespace DPL