format.cxx 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. //
  2. // format.cpp
  3. // logger
  4. //
  5. // Created by Sam Jaffe on 8/21/16.
  6. //
  7. #include "logger/format.h"
  8. #include <cstdint>
  9. #include <cstring>
  10. #include <ctime>
  11. #include <iomanip>
  12. #include <iostream>
  13. #include <map>
  14. #include <stdexcept>
  15. #include <string>
  16. #include "common.h"
  17. #include "logger/exception.h"
  18. #include "logger/logger.h"
  19. #if defined( _WIN32 )
  20. # define NEWLINE "\r\n"
  21. #else
  22. # define NEWLINE "\n"
  23. #endif
  24. namespace logging {
  25. std::string fmt_time_with_milis(struct timeval, std::string const &);
  26. string_generator parse_date_format_string(char const *);
  27. #define is( chr ) *next == chr
  28. #define is_string( str ) ! strncmp(next, str, strlen(str))
  29. string_generator date_token(char const * next) {
  30. std::string predef_format = "%%Y-%%m-%%d %%H:%%M:%%S,%.03d";
  31. if (is_string("{ISO8601}")) {
  32. predef_format = "%%Y-%%m-%%dT%%H:%%M:%%S.%.03dZ";
  33. } else if (is_string("{ABSOLUTE}")) {
  34. predef_format = "%%H:%%M:%%S,%.03d";
  35. } else if (is_string("{DATE}")) {
  36. predef_format = "%%d %%b %%Y %%H:%%M:%%S,%.03d";
  37. } else if (is('{')) {
  38. return parse_date_format_string(next+1);
  39. }
  40. return [=](logpacket const & lp ){
  41. return fmt_time_with_milis(lp.time, predef_format);
  42. };
  43. }
  44. string_generator string_token(std::string str) {
  45. return [=](logpacket const &){
  46. return str;
  47. };
  48. }
  49. string_generator handle( char const * & next ) {
  50. if (is('c')) {
  51. return [](logpacket const & lp){
  52. return lp.logger;
  53. };
  54. } else if (is('p')) {
  55. return [](logpacket const & lp){
  56. return to_string(lp.level, true);
  57. };
  58. } else if (is('m')) {
  59. return [](logpacket const & lp){
  60. return lp.message;
  61. };
  62. } else {
  63. std::string error_msg{"unknown format character: '"};
  64. throw unknown_format_specifier{error_msg + *next + "'"};
  65. }
  66. }
  67. format::generator parse_with_bounds( char const * & next ) {
  68. bool const is_left = *next == '-';
  69. auto align = is_left ? std::left : std::right;
  70. bool const trunc = *next == '.';
  71. if ( is_left || trunc ) ++next;
  72. string_generator gen;
  73. size_t chars = 0;
  74. int min = std::stoi( next, &chars );
  75. size_t max = 0xFFFFFFFF;
  76. next += chars;
  77. if ( trunc ) {
  78. max = min;
  79. min = 0;
  80. } else if ( *next == '.' ) {
  81. max = std::stoi( next + 1, &chars );
  82. next += chars + 1;
  83. }
  84. gen = handle( next );
  85. return [=](logpacket const & lp, std::ostream & out) {
  86. std::string str = gen(lp);
  87. if (str.length() > max)
  88. str.erase(str.begin()+max, str.end());
  89. out << align << std::setw(min) << str;
  90. };
  91. }
  92. format::generator convert( string_generator gen ) {
  93. return [=](logpacket const & lp, std::ostream & out) {
  94. out << gen( lp );
  95. };
  96. }
  97. format format::parse_format_string( std::string const & fmt ) {
  98. format out;
  99. char const * curr = fmt.c_str();
  100. char const * next = nullptr;
  101. char const * const end = curr + fmt.size();
  102. while ((next = std::strchr(curr, '%')) != nullptr) {
  103. ++next;
  104. if (end == next) {
  105. std::string error_msg{"expected format specifier, got end of string"};
  106. throw format_parsing_exception{error_msg}; // TODO
  107. }
  108. if (curr < next-1) {
  109. out.gen.push_back(convert(string_token({curr, next - 1})));
  110. }
  111. if (is('d')) {
  112. ++next;
  113. out.gen.push_back(convert(date_token(next)));
  114. if (is('{')) next = std::strchr(next, '}');
  115. } else if (is('n')) {
  116. out.gen.push_back(convert(string_token(NEWLINE)));
  117. } else if (is('%')) {
  118. out.gen.push_back(convert(string_token("%")));
  119. } else if (is('.') || is('-') || isdigit( *next )) {
  120. out.gen.push_back(parse_with_bounds(next));
  121. } else {
  122. out.gen.push_back(convert(handle(next)));
  123. }
  124. curr = ++next;
  125. }
  126. if (curr < end) {
  127. out.gen.push_back(convert(string_token({curr, end})));
  128. }
  129. return out;
  130. }
  131. #undef is
  132. void format::process(logpacket const & pkt, std::ostream & os) const {
  133. for (auto func : gen) { func(pkt, os); }
  134. }
  135. std::string format::process(logpacket const & pkt) const {
  136. std::stringstream ss;
  137. process(pkt, ss);
  138. return ss.str();
  139. }
  140. void format_msg(std::ostream & os, std::string const & interp, size_t pos,
  141. std::vector<detail::object> const & objs, size_t idx) {
  142. size_t next = interp.find("{}", pos);
  143. os << interp.substr(pos, next);
  144. if (next == std::string::npos) {
  145. return; // throw?
  146. } else if (!strncmp(interp.c_str() + next - 1, "{{}}", 4)) {
  147. format_msg(os, interp, next+2, objs, idx);
  148. } else {
  149. format_msg(os << objs[idx], interp, next+2, objs, idx+1);
  150. }
  151. }
  152. }