Tizen 2.0 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 <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
30 namespace DPL
31 {
32 namespace Log
33 {
34 namespace // anonymous
35 {
36 using namespace DPL::Colors::Text;
37 const char *DEBUG_BEGIN = GREEN_BEGIN;
38 const char *DEBUG_END = GREEN_END;
39 const char *INFO_BEGIN = CYAN_BEGIN;
40 const char *INFO_END = CYAN_END;
41 const char *ERROR_BEGIN = RED_BEGIN;
42 const char *ERROR_END = RED_END;
43 const char *WARNING_BEGIN = BOLD_GOLD_BEGIN;
44 const char *WARNING_END = BOLD_GOLD_END;
45 const char *PEDANTIC_BEGIN = PURPLE_BEGIN;
46 const char *PEDANTIC_END = PURPLE_END;
47
48
49 std::string GetFormattedTime()
50 {
51     timeval tv;
52     tm localNowTime;
53
54     gettimeofday(&tv, NULL);
55     localtime_r(&tv.tv_sec, &localNowTime);
56
57     char format[64];
58     snprintf(format, sizeof(format), "%02i:%02i:%02i.%03i", localNowTime.tm_hour, localNowTime.tm_min, localNowTime.tm_sec, static_cast<int>(tv.tv_usec / 1000));
59     return format;
60 }
61
62 } // namespace anonymous
63
64 std::string OldStyleLogProvider::FormatMessage(const char *message, const char *filename, int line, const char *function)
65 {
66     std::ostringstream val;
67
68     val << std::string("[") << GetFormattedTime() << std::string("] [") <<
69            static_cast<unsigned long>(pthread_self()) << "/" << static_cast<int>(getpid()) << std::string("] [") <<
70            LocateSourceFileName(filename) << std::string(":") << line <<
71            std::string("] ") << function << std::string("(): ") << message;
72
73     return val.str();
74 }
75
76 OldStyleLogProvider::OldStyleLogProvider(bool showDebug, bool showInfo, bool showWarning, bool showError, bool showPedantic)
77     : m_showDebug(showDebug),
78       m_showInfo(showInfo),
79       m_showWarning(showWarning),
80       m_showError(showError),
81       m_showPedantic(showPedantic)
82 {
83 }
84
85 void OldStyleLogProvider::Debug(const char *message, const char *filename, int line, const char *function)
86 {
87     if (m_showDebug)
88         fprintf(stdout, "%s%s%s\n", DEBUG_BEGIN, FormatMessage(message, filename, line, function).c_str(), DEBUG_END);
89 }
90
91 void OldStyleLogProvider::Info(const char *message, const char *filename, int line, const char *function)
92 {
93     if (m_showInfo)
94         fprintf(stdout, "%s%s%s\n", INFO_BEGIN, FormatMessage(message, filename, line, function).c_str(), INFO_END);
95 }
96
97 void OldStyleLogProvider::Warning(const char *message, const char *filename, int line, const char *function)
98 {
99     if (m_showWarning)
100         fprintf(stdout, "%s%s%s\n", WARNING_BEGIN, FormatMessage(message, filename, line, function).c_str(), WARNING_END);
101 }
102
103 void OldStyleLogProvider::Error(const char *message, const char *filename, int line, const char *function)
104 {
105     if (m_showError)
106         fprintf(stdout, "%s%s%s\n", ERROR_BEGIN, FormatMessage(message, filename, line, function).c_str(), ERROR_END);
107 }
108
109 void OldStyleLogProvider::Pedantic(const char *message, const char *filename, int line, const char *function)
110 {
111     if (m_showPedantic)
112         fprintf(stdout, "%s%s%s\n", PEDANTIC_BEGIN, FormatMessage(message, filename, line, function).c_str(), PEDANTIC_END);
113 }
114
115 }
116 } // namespace DPL