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