Skip to content
Snippets Groups Projects
Commit d48291ac authored by Adrien Béraud's avatar Adrien Béraud Committed by Sébastien Blin
Browse files

logger: format header on demand

Change-Id: I6308ad4ed087458e39f7396c7ac223a072093c28
parent 32f39e65
No related branches found
No related tags found
No related merge requests found
...@@ -62,19 +62,6 @@ ...@@ -62,19 +62,6 @@
#endif /* APP_NAME */ #endif /* APP_NAME */
#endif #endif
#define BLACK "\033[22;30m"
#define GREEN "\033[22;32m"
#define BROWN "\033[22;33m"
#define BLUE "\033[22;34m"
#define MAGENTA "\033[22;35m"
#define GREY "\033[22;37m"
#define DARK_GREY "\033[01;30m"
#define LIGHT_RED "\033[01;31m"
#define LIGHT_SCREEN "\033[01;32m"
#define LIGHT_BLUE "\033[01;34m"
#define LIGHT_MAGENTA "\033[01;35m"
#define LIGHT_CYAN "\033[01;36m"
#define WHITE "\033[01;37m"
#define END_COLOR "\033[0m" #define END_COLOR "\033[0m"
#ifndef _WIN32 #ifndef _WIN32
...@@ -133,13 +120,14 @@ strErr() ...@@ -133,13 +120,14 @@ strErr()
static const char* static const char*
stripDirName(const char* path) stripDirName(const char* path)
{ {
if (path) {
const char* occur = strrchr(path, DIR_SEPARATOR_CH); const char* occur = strrchr(path, DIR_SEPARATOR_CH);
return occur ? occur + 1 : path; return occur ? occur + 1 : path;
} else return nullptr;
} }
static std::string std::string
contextHeader(const char* const file, int line) formatHeader(const char* const file, int line)
{ {
#ifdef __linux__ #ifdef __linux__
auto tid = syscall(__NR_gettid) & 0xffff; auto tid = syscall(__NR_gettid) & 0xffff;
...@@ -202,29 +190,37 @@ struct Logger::Msg ...@@ -202,29 +190,37 @@ struct Logger::Msg
Msg() = delete; Msg() = delete;
Msg(int level, const char* file, int line, bool linefeed, std::string&& message) Msg(int level, const char* file, int line, bool linefeed, std::string&& message)
: payload_(std::move(message)) : file_(stripDirName(file))
, header_(contextHeader(file, line)) , line_(line)
, payload_(std::move(message))
, level_(level) , level_(level)
, linefeed_(linefeed) , linefeed_(linefeed)
{} {}
Msg(int level, const char* file, int line, bool linefeed, const char* fmt, va_list ap) Msg(int level, const char* file, int line, bool linefeed, const char* fmt, va_list ap)
: payload_(formatPrintfArgs(fmt, ap)) : file_(stripDirName(file))
, header_(contextHeader(file, line)) , line_(line)
, payload_(formatPrintfArgs(fmt, ap))
, level_(level) , level_(level)
, linefeed_(linefeed) , linefeed_(linefeed)
{} {}
Msg(Msg&& other) Msg(Msg&& other)
{ {
file_ = other.file_;
line_ = other.line_;
payload_ = std::move(other.payload_); payload_ = std::move(other.payload_);
header_ = std::move(other.header_);
level_ = other.level_; level_ = other.level_;
linefeed_ = other.linefeed_; linefeed_ = other.linefeed_;
} }
inline std::string header() const {
return formatHeader(file_, line_);
}
const char* file_;
unsigned line_;
std::string payload_; std::string payload_;
std::string header_;
int level_; int level_;
bool linefeed_; bool linefeed_;
}; };
...@@ -255,7 +251,7 @@ public: ...@@ -255,7 +251,7 @@ public:
} }
#ifdef _WIN32 #ifdef _WIN32
void printLogImpl(jami::Logger::Msg& msg, bool with_color) void printLogImpl(Logger::Msg& msg, bool with_color)
{ {
// If we are using Visual Studio, we can use OutputDebugString to print // If we are using Visual Studio, we can use OutputDebugString to print
// to the "Output" window. Otherwise, we just use fputs to stderr. // to the "Output" window. Otherwise, we just use fputs to stderr.
...@@ -274,6 +270,7 @@ public: ...@@ -274,6 +270,7 @@ public:
WORD saved_attributes; WORD saved_attributes;
static HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE); static HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
auto header = msg.header();
if (with_color) { if (with_color) {
static WORD color_header = CYAN; static WORD color_header = CYAN;
WORD color_prefix = LIGHT_GREEN; WORD color_prefix = LIGHT_GREEN;
...@@ -293,12 +290,12 @@ public: ...@@ -293,12 +290,12 @@ public:
saved_attributes = consoleInfo.wAttributes; saved_attributes = consoleInfo.wAttributes;
SetConsoleTextAttribute(hConsole, color_header); SetConsoleTextAttribute(hConsole, color_header);
printFunc(msg.header_.c_str()); printFunc(header.c_str());
SetConsoleTextAttribute(hConsole, saved_attributes); SetConsoleTextAttribute(hConsole, saved_attributes);
SetConsoleTextAttribute(hConsole, color_prefix); SetConsoleTextAttribute(hConsole, color_prefix);
} else { } else {
printFunc(msg.header_.c_str()); printFunc(header.c_str());
} }
printFunc(msg.payload_.c_str()); printFunc(msg.payload_.c_str());
...@@ -312,8 +309,9 @@ public: ...@@ -312,8 +309,9 @@ public:
} }
} }
#else #else
void printLogImpl(jami::Logger::Msg& msg, bool with_color) void printLogImpl(const Logger::Msg& msg, bool with_color)
{ {
auto header = msg.header();
if (with_color) { if (with_color) {
const char* color_header = CYAN; const char* color_header = CYAN;
const char* color_prefix = ""; const char* color_prefix = "";
...@@ -329,26 +327,25 @@ public: ...@@ -329,26 +327,25 @@ public:
} }
fputs(color_header, stderr); fputs(color_header, stderr);
fputs(msg.header_.c_str(), stderr); fwrite(header.c_str(), 1, header.size(), stderr);
fputs(END_COLOR, stderr); fputs(END_COLOR, stderr);
fputs(color_prefix, stderr); fputs(color_prefix, stderr);
} else { } else {
fputs(msg.header_.c_str(), stderr); fwrite(header.c_str(), 1, header.size(), stderr);
} }
fputs(msg.payload_.c_str(), stderr); fputs(msg.payload_.c_str(), stderr);
if (msg.linefeed_) {
putc(ENDL, stderr);
}
if (with_color) { if (with_color) {
fputs(END_COLOR, stderr); fputs(END_COLOR, stderr);
} }
if (msg.linefeed_) {
putc(ENDL, stderr);
}
} }
#endif /* _WIN32 */ #endif /* _WIN32 */
virtual void consume(jami::Logger::Msg& msg) override void consume(Logger::Msg& msg) override
{ {
static bool with_color = !(getenv("NO_COLOR") || getenv("NO_COLORS") || getenv("NO_COLOUR") static bool with_color = !(getenv("NO_COLOR") || getenv("NO_COLORS") || getenv("NO_COLOUR")
|| getenv("NO_COLOURS")); || getenv("NO_COLOURS"));
...@@ -406,10 +403,10 @@ public: ...@@ -406,10 +403,10 @@ public:
#endif /* _WIN32 */ #endif /* _WIN32 */
} }
virtual void consume(Logger::Msg& msg) override void consume(Logger::Msg& msg) override
{ {
#ifdef __ANDROID__ #ifdef __ANDROID__
__android_log_print(msg.level_, APP_NAME, "%s%s", msg.header_.c_str(), msg.payload_.c_str()); __android_log_write(msg.level_, msg.file_, msg.payload_.c_str());
#else #else
::syslog(msg.level_, "%.*s", (int) msg.payload_.size(), msg.payload_.data()); ::syslog(msg.level_, "%.*s", (int) msg.payload_.size(), msg.payload_.data());
#endif #endif
...@@ -433,15 +430,10 @@ public: ...@@ -433,15 +430,10 @@ public:
return *self; return *self;
} }
virtual void consume(jami::Logger::Msg& msg) override void consume(Logger::Msg& msg) override
{ {
/* auto message = msg.header() + msg.payload_;
* TODO - Maybe change the MessageSend sigature to avoid copying emitSignal<libjami::ConfigurationSignal::MessageSend>(message);
* of message payload?
*/
auto tmp = msg.header_ + msg.payload_;
jami::emitSignal<libjami::ConfigurationSignal::MessageSend>(tmp);
} }
}; };
...@@ -493,6 +485,7 @@ public: ...@@ -493,6 +485,7 @@ public:
do_consume(file, pendingQ_); do_consume(file, pendingQ_);
pendingQ_.clear(); pendingQ_.clear();
} }
file.close();
}); });
} }
...@@ -503,7 +496,7 @@ public: ...@@ -503,7 +496,7 @@ public:
thread_.join(); thread_.join();
} }
virtual void consume(Logger::Msg& msg) override void consume(Logger::Msg& msg) override
{ {
notify([&, this] { currentQ_.emplace_back(std::move(msg)); }); notify([&, this] { currentQ_.emplace_back(std::move(msg)); });
} }
...@@ -520,8 +513,7 @@ private: ...@@ -520,8 +513,7 @@ private:
void do_consume(std::ofstream& file, const std::vector<Logger::Msg>& messages) void do_consume(std::ofstream& file, const std::vector<Logger::Msg>& messages)
{ {
for (const auto& msg : messages) { for (const auto& msg : messages) {
file << msg.header_ << msg.payload_; file << msg.header() << msg.payload_;
if (msg.linefeed_) if (msg.linefeed_)
file << ENDL; file << ENDL;
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment