Skip to content

Commit 55ec716

Browse files
authored
Use colored log output and timestamps in default log handler (#267)
I've had this lying around when implementing things. Maybe that would be useful to have it in the general library.
1 parent fecb175 commit 55ec716

File tree

1 file changed

+10
-5
lines changed

1 file changed

+10
-5
lines changed

src/default_log_handler.cpp

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,29 +30,34 @@
3030

3131
#include "ur_client_library/default_log_handler.h"
3232
#include <stdio.h>
33+
#include <chrono>
34+
#include <string>
3335

3436
namespace urcl
3537
{
38+
3639
DefaultLogHandler::DefaultLogHandler() = default;
3740

3841
void DefaultLogHandler::log(const char* file, int line, LogLevel loglevel, const char* log)
3942
{
43+
auto timestamp = std::chrono::duration<double>(std::chrono::system_clock::now().time_since_epoch());
44+
4045
switch (loglevel)
4146
{
4247
case LogLevel::INFO:
43-
printf("%s%s %i: %s \n", "INFO ", file, line, log);
48+
printf("[%f] %s%s %i: %s \n", timestamp.count(), "INFO ", file, line, log);
4449
break;
4550
case LogLevel::DEBUG:
46-
printf("%s%s %i: %s \n", "DEBUG ", file, line, log);
51+
printf("\033[36m[%f] %s%s %i: %s \033[0m\n", timestamp.count(), "DEBUG ", file, line, log);
4752
break;
4853
case LogLevel::WARN:
49-
printf("%s%s %i: %s \n", "WARN ", file, line, log);
54+
printf("\033[33m[%f] %s%s %i: %s \033[0m\n", timestamp.count(), "WARN ", file, line, log);
5055
break;
5156
case LogLevel::ERROR:
52-
printf("%s%s %i: %s \n", "ERROR ", file, line, log);
57+
printf("\033[31m[%f] %s%s %i: %s \033[0m\n", timestamp.count(), "ERROR ", file, line, log);
5358
break;
5459
case LogLevel::FATAL:
55-
printf("%s%s %i: %s \n", "FATAL ", file, line, log);
60+
printf("\033[31m[%f] %s%s %i: %s \033[0m\n", timestamp.count(), "FATAL ", file, line, log);
5661
break;
5762
default:
5863
break;

0 commit comments

Comments
 (0)