Selaa lähdekoodia

Do some cleanup work

Sam Jaffe 6 vuotta sitten
vanhempi
commit
6baabdcc1e
5 muutettua tiedostoa jossa 29 lisäystä ja 51 poistoa
  1. 2 2
      include/logger/logger.h
  2. 1 5
      src/date_format.cxx
  3. 24 31
      src/format.cxx
  4. 0 1
      src/log_manager.cxx
  5. 2 12
      src/logger.cxx

+ 2 - 2
include/logger/logger.h

@@ -26,9 +26,9 @@
 #include "logger_fwd.h"
 #include "format.h"
 
-#define log_here { __FILE__, __LINE__, STRING( FUNCTION ) }
+#define log_here { __FILE__, __LINE__, STRING(FUNCTION) }
 #define log_message( logger, lvl, ... )   \
-logger.log( level::lvl, log_here, __VA_ARGS__ )
+  logger.log(level::lvl, log_here, __VA_ARGS__)
 
 namespace logging {
   class logger_impl;

+ 1 - 5
src/date_format.cxx

@@ -28,10 +28,6 @@ namespace logging {
     return buf;
   }
   
-  std::string fmt_time(struct timeval round, std::string const & fmt) {
-    return fmt_time(round, fmt.c_str());
-  }
-  
   std::string fmt_time_with_milis(struct timeval round,
                                   std::string const & fmt) {
     char buf[64] = {'\0'};
@@ -52,7 +48,7 @@ namespace logging {
       };
     } else {
       return [=](logpacket const & lp) {
-        return fmt_time(lp.time, fmt);
+        return fmt_time(lp.time, fmt.c_str());
       };
     }
   }

+ 24 - 31
src/format.cxx

@@ -50,55 +50,48 @@ namespace logging {
   }
 
   string_generator string_token(std::string str) {
-    return [=](logpacket const &){
-      return str;
-    };
+    return [=](logpacket const &){ return str; };
   }
   
   string_generator handle( char const * & next ) {
     if (is('c')) {
-      return [](logpacket const & lp){
-        return lp.logger;
-      };
+      return [](logpacket const & lp){ return lp.logger; };
     } else if (is('p')) {
-      return [](logpacket const & lp){
-        return to_string(lp.level, true);
-      };
+      return [](logpacket const & lp){ return to_string(lp.level, true); };
     } else if (is('m')) {
-      return [](logpacket const & lp){
-        return lp.message.str();
-      };
+      return [](logpacket const & lp){ return lp.message.str(); };
     } else {
       std::string error_msg{"unknown format character: '"};
       throw unknown_format_specifier{error_msg + *next + "'"};
     }
   }
   
-  format::generator parse_with_bounds( char const * & next ) {
+  int eat(char const * & token) {
+    size_t chars = 0;
+    int rval = std::stoi(token, &chars);
+    token += chars;
+    return rval;
+  }
+  
+  std::pair<int, size_t> get_bounds(char const * & next) {
+    int min = *next == '.' ? 0 : eat(next);
+    size_t max = *next == '.' ? eat(++next) : std::string::npos;
+    return std::make_pair(min, max);
+  }
+  
+  format::generator parse_with_bounds(char const * & next) {
     bool const is_left = *next == '-';
     auto align = is_left ? std::left : std::right;
-    bool const trunc = *next == '.';
-    if ( is_left || trunc ) ++next;
+    if (is_left) ++next;
     
-    string_generator gen;
-    size_t chars = 0;
-    int min = std::stoi( next, &chars );
-    size_t max = 0xFFFFFFFF;
-    next += chars;
-    if ( trunc ) {
-      max = min;
-      min = 0;
-    } else if ( *next == '.' ) {
-      max = std::stoi( next + 1, &chars );
-      next += chars + 1;
-    }
-    gen = handle( next );
+    auto bounds = get_bounds(next);
+    string_generator gen = handle(next);
     
     return [=](logpacket const & lp, std::ostream & out) {
       std::string str = gen(lp);
-      if (str.length() > max)
-        str.erase(str.begin()+max, str.end());
-      out << align << std::setw(min) << str;
+      if (str.length() > bounds.second)
+        str.erase(str.begin()+bounds.second, str.end());
+      out << align << std::setw(bounds.first) << str;
     };
   }
   

+ 0 - 1
src/log_manager.cxx

@@ -83,7 +83,6 @@ void inject_log_level(properties const & props, T & val) {
 }
 
 void manager_impl::configure_appenders(properties const & props) {
-  // TODO: Load logger_impl here
   // TODO: Support multiple File loggers, etc.
   expects(props["appenders"].type == properties::OBJECT);
   for (auto & app : props["appenders"].obj) {

+ 2 - 12
src/logger.cxx

@@ -7,30 +7,20 @@
 
 #include "logger/logger.h"
 
-#include <sys/time.h>
-
-#include <cassert>
-#include <cmath>
-#include <ctime>
-#include <map>
-#include <stdexcept>
-
 #include "common.h"
 #include "logger/detail/logger_impl.h"
 #include "logger/logpacket.h"
 
 namespace logging {
   logger::logger(std::string const & name, std::shared_ptr<logger_impl> impl)
-  : logger_name_(name), impl_(impl)
-  {
+  : logger_name_(name), impl_(impl) {
   }
   
   logger::~logger() {
     if (impl_) impl_->flush();
   }
   
-  void logger::log(level ll, location_info info,
-                   message const & msg) {
+  void logger::log(level ll, location_info info, message const & msg) {
     impl_->write({ now(), ll, info, logger_name_, msg });
   }