Alex Deymo | a28e019 | 2017-09-08 14:21:05 +0200 | [diff] [blame] | 1 | // Copyright 2017 The Chromium OS Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #ifndef _BSDIFF_LOGGING_H_ |
| 6 | #define _BSDIFF_LOGGING_H_ |
| 7 | |
Tianjie Xu | 18480eb | 2017-11-29 16:21:43 -0800 | [diff] [blame] | 8 | #include <string.h> |
| 9 | |
Alex Deymo | a28e019 | 2017-09-08 14:21:05 +0200 | [diff] [blame] | 10 | #include <iostream> |
Tianjie Xu | 18480eb | 2017-11-29 16:21:43 -0800 | [diff] [blame] | 11 | #include <sstream> |
Alex Deymo | a28e019 | 2017-09-08 14:21:05 +0200 | [diff] [blame] | 12 | |
| 13 | // Simple error logging macro to avoid dependencies in other base libraries. |
Tianjie Xu | 18480eb | 2017-11-29 16:21:43 -0800 | [diff] [blame] | 14 | #define LOG(severity) LogMessage(__FILE__, __LINE__, #severity).stream() |
| 15 | |
| 16 | // A variant of LOG that also logs the current errno value. |
| 17 | #define PLOG(severity) LogMessage(__FILE__, __LINE__, #severity, errno).stream() |
| 18 | |
| 19 | // A temporarily scoped object used by LOG & PLOG. |
| 20 | class LogMessage { |
| 21 | public: |
| 22 | LogMessage(const char* file, unsigned int line, const char* severity); |
| 23 | |
| 24 | LogMessage(const char* file, |
| 25 | unsigned int line, |
| 26 | const char* severity, |
| 27 | int error); |
| 28 | |
| 29 | ~LogMessage(); |
| 30 | |
| 31 | // Returns the stream associated with the message, the LogMessage performs |
| 32 | // output when it goes out of scope. |
| 33 | std::ostream& stream() { return stream_; } |
| 34 | |
| 35 | private: |
| 36 | std::ostringstream stream_; |
| 37 | int error_; // The saved errno value. |
| 38 | }; |
Alex Deymo | a28e019 | 2017-09-08 14:21:05 +0200 | [diff] [blame] | 39 | |
| 40 | #endif // _BSDIFF_LOGGING_H_ |