1 // Copyright (c) 2021 Samsung Electronics Co., Ltd All Rights Reserved
2 // Use of this source code is governed by a apache 2.0 license that can be
3 // found in the LICENSE file.
11 #define PROJECT_TAG "PKGMGR_INFO"
26 #define __FILENAME__ \
27 (strrchr(__FILE__, '/') ? strrchr(__FILE__, '/') + 1 : __FILE__)
39 log_priority LogLevelToPriority(LogLevel level);
41 template <class charT, class traits = std::char_traits<charT>>
42 class StringStream : private std::basic_ostringstream<charT, traits> {
44 using std::basic_ostringstream<charT, traits>::str;
47 StringStream& operator<<(const T& value) {
48 static_cast<std::basic_ostringstream<charT, traits> &>(*this) << value;
55 LogCatcher(LogLevel level, const char* tag)
56 : level_(level), tag_(tag) { }
58 void operator&(const StringStream<char>& str) const {
59 dlog_print(LogLevelToPriority(level_), tag_.c_str(), "%s",
60 Escape(str.str()).c_str());
64 // Since LogCatcher passes input to dlog_print(), the input which contains
65 // format string(such as %d, %n) can cause unexpected result.
66 // This is simple function to escape '%'.
67 // NOTE: Is there any gorgeous way instead of this?
68 std::string Escape(const std::string& str) const {
69 std::string escaped = std::string(str);
71 std::string from = "%";
72 std::string to = "%%";
73 while ((start_pos = escaped.find(from, start_pos)) != std::string::npos) {
74 escaped.replace(start_pos, from.length(), to);
75 start_pos += to.length();
85 inline static const constexpr char* __tag_for_project() {
89 // Simple logging macro of following usage:
90 // LOG(LEVEL) << object_1 << object_2 << object_n;
92 // LEVEL = ERROR | WARNING | INFO | DEBUG
94 ::utils::LogCatcher( \
95 ::utils::LogLevel::LOG_ ## LEVEL, __tag_for_project()) \
96 & ::utils::StringStream<char>() \
97 << std::setw(50) << std::right \
98 << (std::string(__FILENAME__) + ": " + std::string(__FUNCTION__) + "(" + \
99 std::to_string(__LINE__) + ")").c_str() \
100 << std::setw(0) << " : " \
102 #endif // LOGGING_HH_