2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * @file old_style_log_provider.cpp
18 * @author Przemyslaw Dobrowolski (p.dobrowolsk@samsung.com)
20 * @brief This file is the implementation file of old style log provider
31 #include <dpl/log/old_style_log_provider.h>
32 #include <dpl/colors.h>
36 namespace // anonymous
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;
50 std::string GetFormattedTime()
55 gettimeofday(&tv, NULL);
56 localtime_r(&tv.tv_sec, &localNowTime);
61 "%02i:%02i:%02i.%03i",
65 static_cast<int>(tv.tv_usec / 1000));
70 const char *const begin;
71 const char *const end;
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} }
82 } // namespace anonymous
84 OldStyleLogProvider::OldStyleLogProvider() {}
86 void OldStyleLogProvider::Log(AbstractLogProvider::LogLevel level,
90 const char *function) const
93 const struct ColorMark& mark = consoleLevel.at(level);
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);
107 } // namespace VcoreDPL