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 
8 #include <string.h>
9 
10 #include <iostream>
11 #include <sstream>
12 
13 // Simple error logging macro to avoid dependencies in other base libraries.
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 };
39 
40 #endif  // _BSDIFF_LOGGING_H_
41