blob: 0a3bc13a33d1e14ecd870fec6b54eba4a02de61a [file] [log] [blame]
Alex Deymoa28e0192017-09-08 14:21:05 +02001// 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 Xu18480eb2017-11-29 16:21:43 -08008#include <string.h>
9
Alex Deymoa28e0192017-09-08 14:21:05 +020010#include <iostream>
Tianjie Xu18480eb2017-11-29 16:21:43 -080011#include <sstream>
Alex Deymoa28e0192017-09-08 14:21:05 +020012
13// Simple error logging macro to avoid dependencies in other base libraries.
Tianjie Xu18480eb2017-11-29 16:21:43 -080014#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.
20class 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 Deymoa28e0192017-09-08 14:21:05 +020039
40#endif // _BSDIFF_LOGGING_H_