MADARA  3.2.3
Logger.cpp
Go to the documentation of this file.
1 #include "Logger.h"
2 #include <stdio.h>
3 #include <stdarg.h>
4 #include <time.h>
5 
6 madara::logger::Logger::Logger (bool log_to_terminal)
7 : mutex_ (), level_ (LOG_ERROR),
8  term_added_ (log_to_terminal), syslog_added_ (false), tag_ ("madara"),
9  timestamp_format_ ("")
10 {
11  if (log_to_terminal)
12  {
13  add_term ();
14  }
15 }
16 
18 {
19  clear ();
20 }
21 
22 void
23 madara::logger::Logger::log (int level, const char * message, ...)
24 {
25  if (level <= level_)
26  {
27  va_list argptr;
28  va_start (argptr, message);
29 
30  // Android seems to not handle printf arguments correctly as best I can tell
31  char buffer[10240];
32  char * begin = (char *)buffer;
33  size_t remaining_buffer = sizeof (buffer);
34 
35  if (this->timestamp_format_.size () > 0)
36  {
37  time_t rawtime;
38  struct tm * timeinfo;
39 
40  time (&rawtime);
41  timeinfo = localtime (&rawtime);
42 
43  size_t chars_written = strftime (
44  begin, remaining_buffer, timestamp_format_.c_str (), timeinfo);
45 
46  remaining_buffer -= chars_written;
47  begin += chars_written;
48  }
49 
50  vsnprintf (begin, remaining_buffer, message, argptr);
51 
52  va_end (argptr);
53 
54  MADARA_GUARD_TYPE guard (mutex_);
55 
56 #ifdef _MADARA_ANDROID_
57  if (this->term_added_ || this->syslog_added_)
58  {
59  if (level == LOG_ERROR)
60  {
61  __android_log_write (ANDROID_LOG_ERROR, tag_.c_str (), buffer);
62  }
63  else if (level == LOG_WARNING)
64  {
65  __android_log_write (ANDROID_LOG_WARN, tag_.c_str (), buffer);
66  }
67  else
68  {
69  __android_log_write (ANDROID_LOG_INFO, tag_.c_str (), buffer);
70  }
71  }
72 #else // end if _USING_ANDROID_
73  if (this->term_added_ || this->syslog_added_)
74  {
75  fprintf (stderr, "%s", buffer);
76  }
77 #endif
78 
79  int file_num = 0;
80  for (FileVectors::iterator i = files_.begin ();
81  i != files_.end (); ++i)
82  {
83  if (level >= LOG_DETAILED)
84  {
85  fprintf (stderr, "Logger::log: writing to file num %d", file_num);
86 
87  // file_num is only important if logging is detailed
88  ++file_num;
89  }
90  fprintf (*i, "%s", buffer);
91  }
92  }
93 }
std::atomic< int > level_
the maximum detail level for logging
Definition: Logger.h:178
~Logger()
Destructor.
Definition: Logger.cpp:17
std::string timestamp_format_
the timestamp format. Default is "" for no timestamp
Definition: Logger.h:190
bool term_added_
tracks whether terminal output has been added
Definition: Logger.h:181
Logger(bool log_to_stderr=true)
Constructor.
Definition: Logger.cpp:6
FileVectors files_
list of all log outputs
Definition: Logger.h:175
bool syslog_added_
tracks whether the system log has been added
Definition: Logger.h:184
void add_term(void)
Adds terminal to logger outputs.
Definition: Logger.inl:72
void log(int level, const char *message,...)
Logs a message to all available loggers.
Definition: Logger.cpp:23
void clear(void)
Clears all log targets.
Definition: Logger.inl:88
MADARA_LOCK_TYPE mutex_
mutex for changes
Definition: Logger.h:172
std::string tag_
the tag used for logging to system logs
Definition: Logger.h:187