cmake/Source/CPack/cmCPackLog.cxx

165 lines
3.9 KiB
C++
Raw Normal View History

2016-10-30 18:24:19 +01:00
/* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
file Copyright.txt or https://cmake.org/licensing for details. */
#include "cmCPackLog.h"
2017-04-14 19:02:05 +02:00
#include <iostream>
2020-08-30 11:54:41 +02:00
#include <cm/memory>
#include "cmGeneratedFileStream.h"
#include "cmSystemTools.h"
cmCPackLog::cmCPackLog()
{
this->DefaultOutput = &std::cout;
this->DefaultError = &std::cerr;
}
2020-08-30 11:54:41 +02:00
cmCPackLog::~cmCPackLog() = default;
void cmCPackLog::SetLogOutputStream(std::ostream* os)
{
2020-08-30 11:54:41 +02:00
this->LogOutputStream.reset();
this->LogOutput = os;
}
bool cmCPackLog::SetLogOutputFile(const char* fname)
{
2020-08-30 11:54:41 +02:00
this->LogOutputStream.reset();
2016-07-09 11:21:54 +02:00
if (fname) {
2020-08-30 11:54:41 +02:00
this->LogOutputStream = cm::make_unique<cmGeneratedFileStream>(fname);
2016-07-09 11:21:54 +02:00
}
2020-08-30 11:54:41 +02:00
if (this->LogOutputStream && !*this->LogOutputStream) {
this->LogOutputStream.reset();
2016-07-09 11:21:54 +02:00
}
2020-08-30 11:54:41 +02:00
this->LogOutput = this->LogOutputStream.get();
return this->LogOutput != nullptr;
}
2016-07-09 11:21:54 +02:00
void cmCPackLog::Log(int tag, const char* file, int line, const char* msg,
size_t length)
{
// By default no logging
bool display = false;
// Display file and line number if debug
bool useFileAndLine = this->Debug;
2016-07-09 11:21:54 +02:00
bool output = false;
bool debug = false;
bool warning = false;
2016-07-09 11:21:54 +02:00
bool error = false;
bool verbose = false;
// When writing in file, add list of tags whenever tag changes.
std::string tagString;
bool needTagString = false;
2016-07-09 11:21:54 +02:00
if (this->LogOutput && this->LastTag != tag) {
needTagString = true;
2016-07-09 11:21:54 +02:00
}
2016-07-09 11:21:54 +02:00
if (tag & LOG_OUTPUT) {
output = true;
display = true;
2016-07-09 11:21:54 +02:00
if (needTagString) {
if (!tagString.empty()) {
tagString += ",";
}
2020-02-01 23:06:01 +01:00
tagString += "VERBOSE";
}
2016-07-09 11:21:54 +02:00
}
if (tag & LOG_WARNING) {
warning = true;
display = true;
2016-07-09 11:21:54 +02:00
if (needTagString) {
if (!tagString.empty()) {
tagString += ",";
}
2020-02-01 23:06:01 +01:00
tagString += "WARNING";
}
2016-07-09 11:21:54 +02:00
}
if (tag & LOG_ERROR) {
error = true;
display = true;
2016-07-09 11:21:54 +02:00
if (needTagString) {
if (!tagString.empty()) {
tagString += ",";
}
2020-02-01 23:06:01 +01:00
tagString += "ERROR";
}
2016-07-09 11:21:54 +02:00
}
if (tag & LOG_DEBUG && this->Debug) {
debug = true;
display = true;
2016-07-09 11:21:54 +02:00
if (needTagString) {
if (!tagString.empty()) {
tagString += ",";
}
2020-02-01 23:06:01 +01:00
tagString += "DEBUG";
}
2016-07-09 11:21:54 +02:00
useFileAndLine = true;
}
if (tag & LOG_VERBOSE && this->Verbose) {
verbose = true;
display = true;
2016-07-09 11:21:54 +02:00
if (needTagString) {
if (!tagString.empty()) {
tagString += ",";
}
2020-02-01 23:06:01 +01:00
tagString += "VERBOSE";
}
2016-07-09 11:21:54 +02:00
}
if (this->Quiet) {
display = false;
2016-07-09 11:21:54 +02:00
}
if (this->LogOutput) {
if (needTagString) {
*this->LogOutput << "[" << file << ":" << line << " " << tagString
<< "] ";
}
this->LogOutput->write(msg, length);
2016-07-09 11:21:54 +02:00
}
this->LastTag = tag;
2016-07-09 11:21:54 +02:00
if (!display) {
return;
2016-07-09 11:21:54 +02:00
}
if (this->NewLine) {
if (error && !this->ErrorPrefix.empty()) {
2015-04-27 22:25:09 +02:00
*this->DefaultError << this->ErrorPrefix;
2016-07-09 11:21:54 +02:00
} else if (warning && !this->WarningPrefix.empty()) {
2015-04-27 22:25:09 +02:00
*this->DefaultError << this->WarningPrefix;
2016-07-09 11:21:54 +02:00
} else if (output && !this->OutputPrefix.empty()) {
2015-04-27 22:25:09 +02:00
*this->DefaultOutput << this->OutputPrefix;
2016-07-09 11:21:54 +02:00
} else if (verbose && !this->VerbosePrefix.empty()) {
2015-04-27 22:25:09 +02:00
*this->DefaultOutput << this->VerbosePrefix;
2016-07-09 11:21:54 +02:00
} else if (debug && !this->DebugPrefix.empty()) {
2015-04-27 22:25:09 +02:00
*this->DefaultOutput << this->DebugPrefix;
2016-07-09 11:21:54 +02:00
} else if (!this->Prefix.empty()) {
2015-04-27 22:25:09 +02:00
*this->DefaultOutput << this->Prefix;
2016-07-09 11:21:54 +02:00
}
if (useFileAndLine) {
if (error || warning) {
*this->DefaultError << file << ":" << line << " ";
2016-07-09 11:21:54 +02:00
} else {
*this->DefaultOutput << file << ":" << line << " ";
}
}
2016-07-09 11:21:54 +02:00
}
if (error || warning) {
this->DefaultError->write(msg, length);
this->DefaultError->flush();
2016-07-09 11:21:54 +02:00
} else {
this->DefaultOutput->write(msg, length);
this->DefaultOutput->flush();
2016-07-09 11:21:54 +02:00
}
if (msg[length - 1] == '\n' || length > 2) {
this->NewLine = true;
2016-07-09 11:21:54 +02:00
}
2016-07-09 11:21:54 +02:00
if (error) {
2022-08-04 22:12:04 +02:00
cmSystemTools::SetErrorOccurred();
2016-07-09 11:21:54 +02:00
}
}