Refactor log system
[platform/core/security/cert-svc.git] / vcore / src / dpl / 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 <unistd.h>
24 #include <sys/time.h>
25 #include <cstdio>
26 #include <cstring>
27 #include <sstream>
28 #include <stdexcept>
29 #include <map>
30
31 #include <dpl/log/old_style_log_provider.h>
32 #include <dpl/colors.h>
33
34 namespace VcoreDPL {
35 namespace Log {
36 namespace // anonymous
37 {
38 using namespace VcoreDPL::Colors::Text;
39 const char *DEBUG_BEGIN = GREEN_BEGIN;
40 const char *DEBUG_END = GREEN_END;
41 const char *INFO_BEGIN = CYAN_BEGIN;
42 const char *INFO_END = CYAN_END;
43 const char *ERROR_BEGIN = RED_BEGIN;
44 const char *ERROR_END = RED_END;
45 const char *WARNING_BEGIN = BOLD_GOLD_BEGIN;
46 const char *WARNING_END = BOLD_GOLD_END;
47 const char *PEDANTIC_BEGIN = PURPLE_BEGIN;
48 const char *PEDANTIC_END = PURPLE_END;
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,
60              sizeof(format),
61              "%02i:%02i:%02i.%03i",
62              localNowTime.tm_hour,
63              localNowTime.tm_min,
64              localNowTime.tm_sec,
65              static_cast<int>(tv.tv_usec / 1000));
66     return format;
67 }
68
69 struct ColorMark {
70     const char *const begin;
71     const char *const end;
72 };
73
74 std::map<AbstractLogProvider::LogLevel, ColorMark> consoleLevel = {
75         { AbstractLogProvider::LogLevel::Error,     {ERROR_BEGIN,       ERROR_END} },
76         { AbstractLogProvider::LogLevel::Warning,   {WARNING_BEGIN,     WARNING_END} },
77         { AbstractLogProvider::LogLevel::Info,      {INFO_BEGIN,        INFO_END} },
78         { AbstractLogProvider::LogLevel::Debug,     {DEBUG_BEGIN,       DEBUG_END} },
79         { AbstractLogProvider::LogLevel::Pedantic,  {PEDANTIC_BEGIN,    PEDANTIC_END} }
80 };
81
82 } // namespace anonymous
83
84 OldStyleLogProvider::OldStyleLogProvider() {}
85
86 void OldStyleLogProvider::Log(AbstractLogProvider::LogLevel level,
87                               const char *message,
88                               const char *filename,
89                               int line,
90                               const char *function) const
91 {
92     try {
93         const struct ColorMark& mark = consoleLevel.at(level);
94
95         std::ostringstream val;
96         val << mark.begin << std::string("[") << GetFormattedTime() << std::string("] [") <<
97                static_cast<unsigned long>(pthread_self()) << "/" << static_cast<int>(getpid()) <<
98                std::string("] [") << LocateSourceFileName(filename) << std::string(":") << line <<
99                std::string("] ") << function << std::string("(): ") << message << mark.end;
100         fprintf(stdout, "%s\n", val.str().c_str());
101     } catch (const std::out_of_range&) {
102         fprintf(stdout, "Unsupported log level: %d\n", level);
103     }
104 }
105
106 }
107 } // namespace VcoreDPL