/ src / processor / logging.cc
logging.cc
  1  // Copyright 2007 Google LLC
  2  //
  3  // Redistribution and use in source and binary forms, with or without
  4  // modification, are permitted provided that the following conditions are
  5  // met:
  6  //
  7  //     * Redistributions of source code must retain the above copyright
  8  // notice, this list of conditions and the following disclaimer.
  9  //     * Redistributions in binary form must reproduce the above
 10  // copyright notice, this list of conditions and the following disclaimer
 11  // in the documentation and/or other materials provided with the
 12  // distribution.
 13  //     * Neither the name of Google LLC nor the names of its
 14  // contributors may be used to endorse or promote products derived from
 15  // this software without specific prior written permission.
 16  //
 17  // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 18  // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 19  // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 20  // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 21  // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 22  // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 23  // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 24  // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 25  // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 26  // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 27  // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 28  
 29  // logging.cc: Breakpad logging
 30  //
 31  // See logging.h for documentation.
 32  //
 33  // Author: Mark Mentovai
 34  
 35  #ifdef HAVE_CONFIG_H
 36  #include <config.h>  // Must come first
 37  #endif
 38  
 39  #include <assert.h>
 40  #include <errno.h>
 41  #include <string.h>
 42  #include <time.h>
 43  
 44  #include <string>
 45  
 46  #include "common/stdio_wrapper.h"
 47  #include "common/using_std_string.h"
 48  #include "processor/logging.h"
 49  #include "processor/pathname_stripper.h"
 50  
 51  namespace google_breakpad {
 52  
 53  LogStream::LogStream(std::ostream& stream, Severity severity,
 54                       const char* file, int line)
 55      : stream_(stream) {
 56    time_t clock;
 57    time(&clock);
 58    struct tm tm_struct;
 59  #ifdef _WIN32
 60    localtime_s(&tm_struct, &clock);
 61  #else
 62    localtime_r(&clock, &tm_struct);
 63  #endif
 64    char time_string[20];
 65    strftime(time_string, sizeof(time_string), "%Y-%m-%d %H:%M:%S", &tm_struct);
 66  
 67    const char* severity_string = "UNKNOWN_SEVERITY";
 68    switch (severity) {
 69      case SEVERITY_INFO:
 70        severity_string = "INFO";
 71        break;
 72      case SEVERITY_ERROR:
 73        severity_string = "ERROR";
 74        break;
 75      case SEVERITY_CRITICAL:
 76        severity_string = "CRITICAL";
 77        break;
 78    }
 79  
 80    stream_ << time_string << ": " << PathnameStripper::File(file) << ":" <<
 81               line << ": " << severity_string << ": ";
 82  }
 83  
 84  LogStream::~LogStream() {
 85    stream_ << std::endl;
 86  }
 87  
 88  string HexString(uint32_t number) {
 89    char buffer[11];
 90    snprintf(buffer, sizeof(buffer), "0x%x", number);
 91    return string(buffer);
 92  }
 93  
 94  string HexString(uint64_t number) {
 95    char buffer[19];
 96    snprintf(buffer, sizeof(buffer), "0x%" PRIx64, number);
 97    return string(buffer);
 98  }
 99  
100  string HexString(int number) {
101    char buffer[19];
102    snprintf(buffer, sizeof(buffer), "0x%x", number);
103    return string(buffer);
104  }
105  
106  int ErrnoString(string* error_string) {
107    assert(error_string);
108  
109    // strerror isn't necessarily thread-safe.  strerror_r would be preferrable,
110    // but GNU libc uses a nonstandard strerror_r by default, which returns a
111    // char* (rather than an int success indicator) and doesn't necessarily
112    // use the supplied buffer.
113    error_string->assign(strerror(errno));
114    return errno;
115  }
116  
117  }  // namespace google_breakpad