aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/main/cpp/util/bazel_log_handler.cc
blob: b0ebeb49f7a7b710093ef21e9c239f8b3766577e (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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
// 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.

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

#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <sstream>

#include "src/main/cpp/util/file.h"
#include "src/main/cpp/util/logging.h"

namespace blaze_util {

BazelLogHandler::BazelLogHandler()
    : output_stream_set_(false),
      logging_deactivated_(false),
      buffer_stream_(new std::stringstream()),
      output_stream_(),
      owned_output_stream_() {}

BazelLogHandler::~BazelLogHandler() {
  if (!logging_deactivated_) {
    // If SetLoggingOutputStream was never called, dump the buffer to stderr,
    // otherwise, flush the stream.
    if (output_stream_ != nullptr) {
      output_stream_->flush();
    } else if (buffer_stream_ != nullptr) {
      std::cerr << buffer_stream_->rdbuf();
    } else {
      std::cerr << "Illegal state - neither a logfile nor a logbuffer "
                << "existed at program end." << std::endl;
    }
  }
}

void BazelLogHandler::HandleMessage(LogLevel level, const std::string& filename,
                                    int line, const std::string& message) {
  // Select the appropriate stream to log to.
  std::ostream* log_stream;
  if (logging_deactivated_) {
    // Do nothing if the output stream was explicitly deactivated, unless the
    // level is USER, in which case the message is meant to be user-visible
    // regardless of logging settings.
    if (level == LOGLEVEL_USER) {
      std::cerr << message << std::endl;
    }
    return;
  } else if (output_stream_ == nullptr) {
    log_stream = buffer_stream_.get();
  } else {
    log_stream = output_stream_;
  }
  (*log_stream) << "[bazel " << LogLevelName(level) << " " << filename << ":"
                << line << "] " << message << std::endl;

  // If we have a fatal message, we should abort and leave a stack trace -
  // normal exit behavior will be lost, so print this log message out to
  // stderr and avoid loosing the information.
  if (level == LOGLEVEL_FATAL) {
    std::cerr << "[bazel " << LogLevelName(level) << " " << filename << ":"
              << line << "] " << message << std::endl;
    std::abort();
  }
}

void BazelLogHandler::SetOutputStreamToStderr() {
  // Disallow second calls to this, we only intend to support setting the output
  // once, otherwise the buffering will not work as intended and the log will be
  // fragmented.
  BAZEL_CHECK(!output_stream_set_) << "Tried to set log output a second time";
  output_stream_set_ = true;

  FlushBufferToNewStreamAndSet(&std::cerr);
}

void BazelLogHandler::SetOutputStream(
    std::unique_ptr<std::ostream> new_output_stream) {
  // Disallow second calls to this, we only intend to support setting the output
  // once, otherwise the buffering will not work as intended and the log will be
  // fragmented.
  BAZEL_CHECK(!output_stream_set_) << "Tried to set log output a second time";
  output_stream_set_ = true;

  if (new_output_stream == nullptr) {
    logging_deactivated_ = true;
    buffer_stream_ = nullptr;
    return;
  }
  owned_output_stream_ = std::move(new_output_stream);
  FlushBufferToNewStreamAndSet(owned_output_stream_.get());
}

void BazelLogHandler::FlushBufferToNewStreamAndSet(
    std::ostream* new_output_stream) {
  // Flush the buffer to the new stream, and print new log lines to it.
  output_stream_ = new_output_stream;
  if (output_stream_->fail()) {
    // If opening the stream failed, continue buffering and have the logs
    // dump to stderr at shutdown.
    output_stream_ = nullptr;
    BAZEL_LOG(ERROR) << "Provided stream failed.";
  } else {
    // Transfer the contents of the buffer to the new stream, then remove the
    // buffer.
    (*output_stream_) << buffer_stream_->str();
    buffer_stream_ = nullptr;
    output_stream_->flush();
  }
}

}  // namespace blaze_util