aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/main/cpp/util/logging.cc
blob: 71c9a669f5960242bcf667361d200389bcb0fa7a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
// Copyright 2016 The Bazel Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//    http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// This file is based off the logging work by the protobuf team
#include "src/main/cpp/util/logging.h"

#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <memory>

#include "src/main/cpp/util/exit_code.h"

namespace blaze_util {

const char* LogLevelName(LogLevel level) {
  static const char* level_names[] = {"INFO", "USER", "WARNING", "ERROR",
                                      "FATAL"};
  BAZEL_CHECK(static_cast<int>(level) < 5)
      << "LogLevelName: level out of range, there are only 5 levels.";
  return level_names[level];
}

namespace internal {

static std::unique_ptr<LogHandler> log_handler_(nullptr);

LogMessage::LogMessage(LogLevel level, const std::string& filename, int line)
    : level_(level), filename_(filename), line_(line) {}

#undef DECLARE_STREAM_OPERATOR
#define DECLARE_STREAM_OPERATOR(TYPE)              \
  LogMessage& LogMessage::operator<<(TYPE value) { \
    message_ << value;                             \
    return *this;                                  \
  }

DECLARE_STREAM_OPERATOR(const std::string&)
DECLARE_STREAM_OPERATOR(const char*)
DECLARE_STREAM_OPERATOR(char)
DECLARE_STREAM_OPERATOR(bool)
DECLARE_STREAM_OPERATOR(short)
DECLARE_STREAM_OPERATOR(int)
DECLARE_STREAM_OPERATOR(unsigned int)
DECLARE_STREAM_OPERATOR(long)
DECLARE_STREAM_OPERATOR(unsigned long)
DECLARE_STREAM_OPERATOR(long long)
DECLARE_STREAM_OPERATOR(unsigned long long)
DECLARE_STREAM_OPERATOR(float)
DECLARE_STREAM_OPERATOR(double)
DECLARE_STREAM_OPERATOR(long double)
DECLARE_STREAM_OPERATOR(void*)
#undef DECLARE_STREAM_OPERATOR

void LogMessage::Finish() {
  std::string message(message_.str());
  if (log_handler_ != nullptr) {
    log_handler_->HandleMessage(level_, filename_, line_, message);
  } else {
    // If no custom handler was provided, never print INFO messages,
    // but USER should always be printed, as should warnings and errors.
    if (level_ == LOGLEVEL_USER) {
      std::cerr << message << std::endl;
    } else if (level_ > LOGLEVEL_USER) {
      std::cerr << LogLevelName(level_) << ": " << message << std::endl;
    }

    if (level_ == LOGLEVEL_FATAL) {
      // Exit for fatal calls after handling the message.
      // TODO(b/32967056) pass correct exit code information.
      std::exit(blaze_exit_code::INTERNAL_ERROR);
    }
  }
}

void LogFinisher::operator=(LogMessage& other) { other.Finish(); }

}  // namespace internal

void SetLogHandler(std::unique_ptr<LogHandler> new_handler) {
  internal::log_handler_ = std::move(new_handler);
}

void SetLoggingOutputStream(std::unique_ptr<std::ostream> output_stream) {
  if (internal::log_handler_ != nullptr) {
    internal::log_handler_->SetOutputStream(std::move(output_stream));
  }
}
void SetLoggingOutputStreamToStderr() {
  if (internal::log_handler_ != nullptr) {
    internal::log_handler_->SetOutputStreamToStderr();
  }
}

}  // namespace blaze_util