PageRenderTime 35ms CodeModel.GetById 14ms app.highlight 16ms RepoModel.GetById 1ms app.codeStats 0ms

/thirdparty/breakpad/processor/logging.cc

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