You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@trafficserver.apache.org by am...@apache.org on 2018/12/25 17:00:51 UTC

[trafficserver] branch master updated: BWF: Clean up diags log entry header construction.

This is an automated email from the ASF dual-hosted git repository.

amc pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/trafficserver.git


The following commit(s) were added to refs/heads/master by this push:
     new 786a38b  BWF: Clean up diags log entry header construction.
786a38b is described below

commit 786a38beba6a48aebcbc564d6ec05c02da5cb28c
Author: Alan M. Carroll <am...@apache.org>
AuthorDate: Wed Nov 14 16:43:41 2018 -0600

    BWF: Clean up diags log entry header construction.
---
 src/tscore/Diags.cc | 74 +++++++++++------------------------------------------
 1 file changed, 15 insertions(+), 59 deletions(-)

diff --git a/src/tscore/Diags.cc b/src/tscore/Diags.cc
index 179be9e..b34db02 100644
--- a/src/tscore/Diags.cc
+++ b/src/tscore/Diags.cc
@@ -34,6 +34,8 @@
 
  ****************************************************************************/
 
+#include "tscore/BufferWriter.h"
+#include "tscore/bwf_std_format.h"
 #include "tscore/ink_platform.h"
 #include "tscore/ink_memory.h"
 #include "tscore/ink_defs.h"
@@ -215,75 +217,29 @@ Diags::print_va(const char *debug_tag, DiagsLevel diags_level, const SourceLocat
                 va_list ap) const
 {
   ink_release_assert(diags_level < DiagsLevel_Count);
-
-  using ts::LocalBufferWriter;
-  LocalBufferWriter<1024> format_writer;
+  ts::LocalBufferWriter<1024> format_writer;
 
   // Save room for optional newline and terminating NUL bytes.
   format_writer.clip(2);
 
-  //////////////////////
-  // append timestamp //
-  //////////////////////
-  {
-    struct timeval tp = ink_gettimeofday();
-    time_t cur_clock  = (time_t)tp.tv_sec;
-    char timestamp_buf[48];
-    char *buffer = ink_ctime_r(&cur_clock, timestamp_buf);
-
-    int num_bytes_written = snprintf(&(timestamp_buf[19]), (sizeof(timestamp_buf) - 20), ".%03d", (int)(tp.tv_usec / 1000));
-
-    if (num_bytes_written > 0) {
-      format_writer.write('[');
-      format_writer.write(buffer + 4, strlen(buffer + 4));
-      format_writer.write("] ", 2);
-    }
-  }
-
-  size_t timestamp_end_offset = format_writer.size();
+  format_writer.print("[{timestamp}] ");
+  auto timestamp_offset = format_writer.size();
 
-  ///////////////////////
-  // add the thread name //
-  ///////////////////////
-  format_writer.print("{thread-name} ");
-
-  //////////////////////////////////
-  // append the diag level prefix //
-  //////////////////////////////////
-
-  format_writer.write(level_name(diags_level), strlen(level_name(diags_level)));
-  format_writer.write(": ", 2);
-
-  /////////////////////////////
-  // append location, if any //
-  /////////////////////////////
+  format_writer.print("{thread-name}");
+  format_writer.print(" {}: ", level_name(diags_level));
 
   if (location(loc, show_location, diags_level)) {
-    char *lp, buf[256];
-    lp = loc->str(buf, sizeof(buf));
-    if (lp) {
-      format_writer.write('<');
-      format_writer.write(lp, std::min(strlen(lp), sizeof(buf)));
-      format_writer.write("> ", 2);
-    }
+    format_writer.print("<{}> ", *loc);
   }
-  //////////////////////////
-  // append debugging tag //
-  //////////////////////////
 
-  if (debug_tag != nullptr) {
-    format_writer.write('(');
-    format_writer.write(debug_tag, strlen(debug_tag));
-    format_writer.write(") ", 2);
+  if (debug_tag) {
+    format_writer.print("({}) ", debug_tag);
   }
-  //////////////////////////////////////////////////////
-  // append original format string, ensure there is a //
-  // newline, and NUL terminate                       //
-  //////////////////////////////////////////////////////
 
-  format_writer.write(format_string, strlen(format_string));
-  format_writer.extend(2);
-  if (format_writer.data()[format_writer.size() - 1] != '\n') {
+  format_writer.print("{}", format_string);
+
+  format_writer.extend(2);                   // restore the space for required termination.
+  if (format_writer.view().back() != '\n') { // safe because always some chars in the buffer.
     format_writer.write('\n');
   }
   format_writer.write('\0');
@@ -359,7 +315,7 @@ Diags::print_va(const char *debug_tag, DiagsLevel diags_level, const SourceLocat
       priority = LOG_NOTICE;
       break;
     }
-    vsnprintf(syslog_buffer, sizeof(syslog_buffer), format_writer.data() + timestamp_end_offset, ap);
+    vsnprintf(syslog_buffer, sizeof(syslog_buffer), format_writer.data() + timestamp_offset, ap);
     syslog(priority, "%s", syslog_buffer);
   }