• Main Page
  • Related Pages
  • Namespaces
  • Data Structures
  • Files
  • File List
  • Globals

TsErrataUtil.cc

Go to the documentation of this file.
00001 /** @file
00002 
00003     TS Configuration utilities for Errata and logging.
00004 
00005     @section license License
00006 
00007     Licensed to the Apache Software Foundation (ASF) under one
00008     or more contributor license agreements.  See the NOTICE file
00009     distributed with this work for additional information
00010     regarding copyright ownership.  The ASF licenses this file
00011     to you under the Apache License, Version 2.0 (the
00012     "License"); you may not use this file except in compliance
00013     with the License.  You may obtain a copy of the License at
00014 
00015     http://www.apache.org/licenses/LICENSE-2.0
00016 
00017     Unless required by applicable law or agreed to in writing, software
00018     distributed under the License is distributed on an "AS IS" BASIS,
00019     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00020     See the License for the specific language governing permissions and
00021     limitations under the License.
00022  */
00023 
00024 # if !defined(_MSC_VER)
00025 # include <stdio.h>
00026 # include <string.h>
00027 # endif
00028 # include <stdarg.h>
00029 # include <errno.h>
00030 # include <TsErrataUtil.h>
00031 # include "ts/ink_string.h"
00032 # include "ts/ink_defs.h"
00033 
00034 namespace ts { namespace msg {
00035 
00036 Errata::Code FATAL = 3; ///< Fatal, cannot continue.
00037 Errata::Code WARN = 2; ///< Significant, should be fixed.
00038 Errata::Code INFO = 1; /// Interesting, not necessarily a problem.
00039 Errata::Code DEBUG = 0; /// Debugging information.
00040 
00041 # if defined(_MSC_VER)
00042 char* strerror_r(int err, char* s, size_t n) {
00043     ink_strlcpy(s, strerror(err), n);
00044     return s;
00045 }
00046 
00047 # define snprintf _snprintf
00048 # endif
00049 
00050 Errata&
00051 log(Errata& err, Errata::Id id, Errata::Code code, char const* text) {
00052   err.push(id, code, text);
00053   return err;
00054 }
00055 
00056 Errata&
00057 log(Errata& err, Errata::Code code, char const* text) {
00058   err.push(0, code, text);
00059   return err;
00060 }
00061 
00062 Errata&
00063 log(RvBase& rv, Errata::Code code, char const* text) {
00064     rv._errata.push(0, code, text);
00065     return rv._errata;
00066 }
00067 
00068 Errata
00069 log(Errata::Code code, char const* text) {
00070   Errata err;
00071   err.push(0, code, text);
00072   return err;
00073 }
00074 
00075 Errata&
00076 vlogf(
00077   Errata& err,
00078   Errata::Id id,
00079   Errata::Code code,
00080   char const* format,
00081   va_list& rest
00082 ) {
00083   static size_t const SIZE = 8192;
00084   char buffer[SIZE];
00085   
00086   vsnprintf(buffer, SIZE, format, rest);
00087   err.push(id, code, buffer);
00088   return err;
00089 }
00090 
00091 Errata&
00092 logf(
00093   Errata& err,
00094   Errata::Id id,
00095   Errata::Code code,
00096   char const* format,
00097   ...
00098 ) {
00099   va_list rest;
00100   va_start(rest, format);
00101   return vlogf(err, id, code, format, rest);
00102 }
00103 
00104 Errata
00105 logf(Errata::Code code, char const* format, ...) {
00106   Errata err;
00107   va_list rest;
00108   va_start(rest, format);
00109   return vlogf(err, Errata::Id(0), code, format, rest);
00110 }
00111 
00112 Errata&
00113 logf(Errata& err, Errata::Code code, char const* format, ...) {
00114   va_list rest;
00115   va_start(rest, format);
00116   return vlogf(err, Errata::Id(0), code, format, rest);
00117 }
00118 
00119 Errata&
00120 logf(RvBase& base, Errata::Code code, char const* format, ...) {
00121     va_list rest;
00122     va_start(rest, format);
00123     return vlogf(base._errata, Errata::Id(0), code, format, rest);
00124 }
00125 
00126 Errata
00127 log_errno(Errata::Code code, char const* text) {
00128   static size_t const SIZE = 1024;
00129   char buffer[SIZE];
00130   ATS_UNUSED_RETURN(strerror_r(errno, buffer, SIZE));
00131   return logf(code, "%s [%d] %s", text, errno, buffer);
00132 }
00133 
00134 Errata
00135 vlogf_errno(Errata& errata, Errata::Id id, Errata::Code code, char const* format, va_list& rest) {
00136   int e = errno; // Preserve value before making system calls.
00137   int n;
00138   static int const E_SIZE = 256;
00139   char e_buffer[E_SIZE];
00140   static int const T_SIZE = 8192;
00141   char t_buffer[T_SIZE];
00142   
00143   n = vsnprintf(t_buffer, T_SIZE, format, rest);
00144   if (0 <= n && n < T_SIZE) { // still have room.
00145     ATS_UNUSED_RETURN(strerror_r(e, e_buffer, E_SIZE));
00146     n += snprintf(t_buffer + n, T_SIZE - n, "[%d] %s", e, e_buffer);
00147   }
00148   errata.push(id, code, t_buffer);
00149   return errata;
00150 }
00151 
00152 Errata
00153 logf_errno(Errata::Code code, char const* format, ...) {
00154   Errata zret;
00155   va_list rest;
00156   va_start(rest, format);
00157   return vlogf_errno(zret, 0, code, format, rest);
00158 }
00159 
00160 Errata
00161 logf_errno(Errata& errata, Errata::Code code, char const* format, ...) {
00162   va_list rest;
00163   va_start(rest, format);
00164   return vlogf_errno(errata, 0, code, format, rest);
00165 }
00166 
00167 Errata
00168 logf_errno(RvBase& rv, Errata::Code code, char const* format, ...) {
00169     va_list rest;
00170     va_start(rest, format);
00171     return vlogf_errno(rv._errata, 0, code, format, rest);
00172 }
00173 // ------------------------------------------------------
00174 }} // namespace ts::msg

Generated by  doxygen 1.7.1