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
23 #include <dpl/log/old_style_log_provider.h>
24 #include <dpl/colors.h>
35 namespace // anonymous
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;
50 std::string GetFormattedTime()
55 gettimeofday(&tv, NULL);
56 localtime_r(&tv.tv_sec, &localNowTime);
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));
63 } // namespace anonymous
65 std::string OldStyleLogProvider::FormatMessage(const char *message, const char *filename, int line, const char *function)
67 std::ostringstream val;
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;
77 OldStyleLogProvider::OldStyleLogProvider(bool showDebug, bool showInfo, bool showWarning, bool showError, bool showPedantic)
78 : m_showDebug(showDebug),
80 m_showWarning(showWarning),
81 m_showError(showError),
82 m_showPedantic(showPedantic)
86 void OldStyleLogProvider::Debug(const char *message, const char *filename, int line, const char *function)
89 fprintf(stdout, "%s%s%s\n", DEBUG_BEGIN, FormatMessage(message, filename, line, function).c_str(), DEBUG_END);
92 void OldStyleLogProvider::Info(const char *message, const char *filename, int line, const char *function)
95 fprintf(stdout, "%s%s%s\n", INFO_BEGIN, FormatMessage(message, filename, line, function).c_str(), INFO_END);
98 void OldStyleLogProvider::Warning(const char *message, const char *filename, int line, const char *function)
101 fprintf(stdout, "%s%s%s\n", WARNING_BEGIN, FormatMessage(message, filename, line, function).c_str(), WARNING_END);
104 void OldStyleLogProvider::Error(const char *message, const char *filename, int line, const char *function)
107 fprintf(stdout, "%s%s%s\n", ERROR_BEGIN, FormatMessage(message, filename, line, function).c_str(), ERROR_END);
110 void OldStyleLogProvider::Pedantic(const char *message, const char *filename, int line, const char *function)
113 fprintf(stdout, "%s%s%s\n", PEDANTIC_BEGIN, FormatMessage(message, filename, line, function).c_str(), PEDANTIC_END);