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