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
22 #include <dpl/log/old_style_log_provider.h>
23 #include <dpl/colors.h>
33 namespace // anonymous
35 using namespace DPL::Colors::Text;
36 const char *DEBUG_BEGIN = GREEN_BEGIN;
37 const char *DEBUG_END = GREEN_END;
38 const char *INFO_BEGIN = CYAN_BEGIN;
39 const char *INFO_END = CYAN_END;
40 const char *ERROR_BEGIN = RED_BEGIN;
41 const char *ERROR_END = RED_END;
42 const char *WARNING_BEGIN = BOLD_GOLD_BEGIN;
43 const char *WARNING_END = BOLD_GOLD_END;
44 const char *PEDANTIC_BEGIN = PURPLE_BEGIN;
45 const char *PEDANTIC_END = PURPLE_END;
48 std::string GetFormattedTime()
53 gettimeofday(&tv, NULL);
54 localtime_r(&tv.tv_sec, &localNowTime);
57 snprintf(format, sizeof(format), "%02i:%02i:%02i.%03i", localNowTime.tm_hour, localNowTime.tm_min, localNowTime.tm_sec, static_cast<int>(tv.tv_usec / 1000));
61 } // namespace anonymous
63 std::string OldStyleLogProvider::FormatMessage(const char *message, const char *filename, int line, const char *function)
65 std::ostringstream val;
67 val << std::string("[") << GetFormattedTime() << std::string("] [") <<
68 static_cast<unsigned long>(pthread_self()) << "/" << static_cast<int>(getpid()) << std::string("] [") <<
69 LocateSourceFileName(filename) << std::string(":") << line <<
70 std::string("] ") << function << std::string("(): ") << message;
75 OldStyleLogProvider::OldStyleLogProvider(bool showDebug, bool showInfo, bool showWarning, bool showError, bool showPedantic)
76 : m_showDebug(showDebug),
78 m_showWarning(showWarning),
79 m_showError(showError),
80 m_showPedantic(showPedantic)
84 void OldStyleLogProvider::Debug(const char *message, const char *filename, int line, const char *function)
87 fprintf(stdout, "%s%s%s\n", DEBUG_BEGIN, FormatMessage(message, filename, line, function).c_str(), DEBUG_END);
90 void OldStyleLogProvider::Info(const char *message, const char *filename, int line, const char *function)
93 fprintf(stdout, "%s%s%s\n", INFO_BEGIN, FormatMessage(message, filename, line, function).c_str(), INFO_END);
96 void OldStyleLogProvider::Warning(const char *message, const char *filename, int line, const char *function)
99 fprintf(stdout, "%s%s%s\n", WARNING_BEGIN, FormatMessage(message, filename, line, function).c_str(), WARNING_END);
102 void OldStyleLogProvider::Error(const char *message, const char *filename, int line, const char *function)
105 fprintf(stdout, "%s%s%s\n", ERROR_BEGIN, FormatMessage(message, filename, line, function).c_str(), ERROR_END);
108 void OldStyleLogProvider::Pedantic(const char *message, const char *filename, int line, const char *function)
111 fprintf(stdout, "%s%s%s\n", PEDANTIC_BEGIN, FormatMessage(message, filename, line, function).c_str(), PEDANTIC_END);