/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. // logging.cc: Breakpad logging
  30. //
  31. // See logging.h for documentation.
  32. //
  33. // Author: Mark Mentovai
  34. #include <assert.h>
  35. #include <errno.h>
  36. #include <stdio.h>
  37. #include <string.h>
  38. #include <time.h>
  39. #include "processor/logging.h"
  40. #include "processor/pathname_stripper.h"
  41. #ifdef _WIN32
  42. #define snprintf _snprintf
  43. #endif
  44. namespace google_breakpad {
  45. LogStream::LogStream(std::ostream &stream, Severity severity,
  46. const char *file, int line)
  47. : stream_(stream) {
  48. time_t clock;
  49. time(&clock);
  50. struct tm tm_struct;
  51. #ifdef _WIN32
  52. localtime_s(&tm_struct, &clock);
  53. #else
  54. localtime_r(&clock, &tm_struct);
  55. #endif
  56. char time_string[20];
  57. strftime(time_string, sizeof(time_string), "%Y-%m-%d %H:%M:%S", &tm_struct);
  58. const char *severity_string = "UNKNOWN_SEVERITY";
  59. switch (severity) {
  60. case SEVERITY_INFO:
  61. severity_string = "INFO";
  62. break;
  63. case SEVERITY_ERROR:
  64. severity_string = "ERROR";
  65. break;
  66. }
  67. stream_ << time_string << ": " << PathnameStripper::File(file) << ":" <<
  68. line << ": " << severity_string << ": ";
  69. }
  70. LogStream::~LogStream() {
  71. stream_ << std::endl;
  72. }
  73. std::string HexString(u_int32_t number) {
  74. char buffer[11];
  75. snprintf(buffer, sizeof(buffer), "0x%x", number);
  76. return std::string(buffer);
  77. }
  78. std::string HexString(u_int64_t number) {
  79. char buffer[19];
  80. snprintf(buffer, sizeof(buffer), "0x%" PRIx64, number);
  81. return std::string(buffer);
  82. }
  83. std::string HexString(int number) {
  84. char buffer[19];
  85. snprintf(buffer, sizeof(buffer), "0x%x", number);
  86. return std::string(buffer);
  87. }
  88. int ErrnoString(std::string *error_string) {
  89. assert(error_string);
  90. // strerror isn't necessarily thread-safe. strerror_r would be preferrable,
  91. // but GNU libc uses a nonstandard strerror_r by default, which returns a
  92. // char* (rather than an int success indicator) and doesn't necessarily
  93. // use the supplied buffer.
  94. error_string->assign(strerror(errno));
  95. return errno;
  96. }
  97. } // namespace google_breakpad