cmake/Source/CPack/cmCPackLog.cxx

182 lines
4.2 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-07-20 19:35:53 +02:00
#include "cmConfigure.h"
2017-04-14 19:02:05 +02:00
#include <iostream>
#include "cmGeneratedFileStream.h"
#include "cmSystemTools.h"
cmCPackLog::cmCPackLog()
{
this->Verbose = false;
this->Debug = false;
this->Quiet = false;
this->NewLine = true;
this->LastTag = cmCPackLog::NOTAG;
this->DefaultOutput = &std::cout;
this->DefaultError = &std::cerr;
2016-10-30 18:24:19 +01:00
this->LogOutput = CM_NULLPTR;
this->LogOutputCleanup = false;
}
cmCPackLog::~cmCPackLog()
{
2016-10-30 18:24:19 +01:00
this->SetLogOutputStream(CM_NULLPTR);
}
void cmCPackLog::SetLogOutputStream(std::ostream* os)
{
2016-07-09 11:21:54 +02:00
if (this->LogOutputCleanup && this->LogOutput) {
delete this->LogOutput;
2016-07-09 11:21:54 +02:00
}
this->LogOutputCleanup = false;
this->LogOutput = os;
}
bool cmCPackLog::SetLogOutputFile(const char* fname)
{
2016-10-30 18:24:19 +01:00
cmGeneratedFileStream* cg = CM_NULLPTR;
2016-07-09 11:21:54 +02:00
if (fname) {
cg = new cmGeneratedFileStream(fname);
2016-07-09 11:21:54 +02:00
}
if (cg && !*cg) {
delete cg;
2016-10-30 18:24:19 +01:00
cg = CM_NULLPTR;
2016-07-09 11:21:54 +02:00
}
this->SetLogOutputStream(cg);
2016-07-09 11:21:54 +02:00
if (!cg) {
return false;
2016-07-09 11:21:54 +02:00
}
this->LogOutputCleanup = true;
return true;
}
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 += ",";
}
2016-07-09 11:21:54 +02: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 += ",";
}
2016-07-09 11:21:54 +02: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 += ",";
}
2016-07-09 11:21:54 +02: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 += ",";
}
2016-07-09 11:21:54 +02: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 += ",";
}
2016-07-09 11:21:54 +02: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) {
cmSystemTools::SetErrorOccured();
2016-07-09 11:21:54 +02:00
}
}