CMake/Source/CPack/cmCPackLog.cxx

191 lines
4.5 KiB
C++
Raw Normal View History

/*============================================================================
CMake - Cross Platform Makefile Generator
Copyright 2000-2009 Kitware, Inc., Insight Software Consortium
2006-01-03 00:14:21 +03:00
Distributed under the OSI-approved BSD License (the "License");
see accompanying file Copyright.txt for details.
2006-01-03 00:14:21 +03:00
This software is distributed WITHOUT ANY WARRANTY; without even the
implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the License for more information.
============================================================================*/
2006-01-03 00:14:21 +03:00
#include "cmCPackLog.h"
#include "cmGeneratedFileStream.h"
#include "cmSystemTools.h"
2006-01-03 00:14:21 +03:00
cmCPackLog::cmCPackLog()
{
2006-03-10 21:06:26 +03:00
this->Verbose = false;
this->Debug = false;
this->Quiet = false;
this->NewLine = true;
2006-01-03 00:14:21 +03:00
2006-03-10 21:06:26 +03:00
this->LastTag = cmCPackLog::NOTAG;
2006-01-03 00:14:21 +03:00
#undef cerr
#undef cout
2006-03-10 21:06:26 +03:00
this->DefaultOutput = &std::cout;
this->DefaultError = &std::cerr;
this->LogOutput = 0;
this->LogOutputCleanup = false;
2006-01-03 00:14:21 +03:00
}
cmCPackLog::~cmCPackLog()
{
this->SetLogOutputStream(0);
}
2006-01-03 01:22:39 +03:00
void cmCPackLog::SetLogOutputStream(std::ostream* os)
2006-01-03 00:14:21 +03:00
{
if (this->LogOutputCleanup && this->LogOutput) {
2006-03-10 21:06:26 +03:00
delete this->LogOutput;
}
2006-03-10 21:06:26 +03:00
this->LogOutputCleanup = false;
this->LogOutput = os;
2006-01-03 00:14:21 +03:00
}
bool cmCPackLog::SetLogOutputFile(const char* fname)
{
cmGeneratedFileStream* cg = 0;
if (fname) {
2006-01-03 00:14:21 +03:00
cg = new cmGeneratedFileStream(fname);
}
if (cg && !*cg) {
2006-01-03 00:14:21 +03:00
delete cg;
cg = 0;
}
2006-01-03 00:14:21 +03:00
this->SetLogOutputStream(cg);
if (!cg) {
2006-01-03 00:14:21 +03:00
return false;
}
2006-03-10 21:06:26 +03:00
this->LogOutputCleanup = true;
2006-01-03 00:14:21 +03:00
return true;
}
void cmCPackLog::Log(int tag, const char* file, int line, const char* msg,
size_t length)
2006-01-03 00:14:21 +03:00
{
// By default no logging
bool display = false;
// Display file and line number if debug
2006-03-10 21:06:26 +03:00
bool useFileAndLine = this->Debug;
2006-01-03 00:14:21 +03:00
bool output = false;
bool debug = false;
2006-01-03 00:14:21 +03:00
bool warning = false;
bool error = false;
2006-01-03 00:14:21 +03:00
bool verbose = false;
// When writing in file, add list of tags whenever tag changes.
std::string tagString;
bool needTagString = false;
if (this->LogOutput && this->LastTag != tag) {
2006-01-03 00:14:21 +03:00
needTagString = true;
}
2006-01-03 00:14:21 +03:00
if (tag & LOG_OUTPUT) {
2006-01-03 00:14:21 +03:00
output = true;
display = true;
if (needTagString) {
if (!tagString.empty()) {
tagString += ",";
2006-01-03 00:14:21 +03:00
}
tagString = "VERBOSE";
2006-01-03 00:14:21 +03:00
}
}
if (tag & LOG_WARNING) {
2006-01-03 00:14:21 +03:00
warning = true;
display = true;
if (needTagString) {
if (!tagString.empty()) {
tagString += ",";
2006-01-03 00:14:21 +03:00
}
tagString = "WARNING";
2006-01-03 00:14:21 +03:00
}
}
if (tag & LOG_ERROR) {
2006-01-03 00:14:21 +03:00
error = true;
display = true;
if (needTagString) {
if (!tagString.empty()) {
tagString += ",";
2006-01-03 00:14:21 +03:00
}
tagString = "ERROR";
2006-01-03 00:14:21 +03:00
}
}
if (tag & LOG_DEBUG && this->Debug) {
2006-01-03 00:14:21 +03:00
debug = true;
display = true;
if (needTagString) {
if (!tagString.empty()) {
tagString += ",";
2006-01-03 00:14:21 +03:00
}
tagString = "DEBUG";
2006-01-03 00:14:21 +03:00
}
useFileAndLine = true;
}
if (tag & LOG_VERBOSE && this->Verbose) {
2006-01-03 00:14:21 +03:00
verbose = true;
display = true;
if (needTagString) {
if (!tagString.empty()) {
tagString += ",";
2006-01-03 00:14:21 +03:00
}
tagString = "VERBOSE";
2006-01-03 00:14:21 +03:00
}
}
if (this->Quiet) {
2006-01-03 00:14:21 +03:00
display = false;
}
if (this->LogOutput) {
if (needTagString) {
*this->LogOutput << "[" << file << ":" << line << " " << tagString
<< "] ";
2006-01-03 00:14:21 +03:00
}
2006-03-10 21:06:26 +03:00
this->LogOutput->write(msg, length);
}
2006-03-10 21:06:26 +03:00
this->LastTag = tag;
if (!display) {
2006-01-03 00:14:21 +03:00
return;
}
if (this->NewLine) {
if (error && !this->ErrorPrefix.empty()) {
*this->DefaultError << this->ErrorPrefix;
} else if (warning && !this->WarningPrefix.empty()) {
*this->DefaultError << this->WarningPrefix;
} else if (output && !this->OutputPrefix.empty()) {
*this->DefaultOutput << this->OutputPrefix;
} else if (verbose && !this->VerbosePrefix.empty()) {
*this->DefaultOutput << this->VerbosePrefix;
} else if (debug && !this->DebugPrefix.empty()) {
*this->DefaultOutput << this->DebugPrefix;
} else if (!this->Prefix.empty()) {
*this->DefaultOutput << this->Prefix;
}
if (useFileAndLine) {
if (error || warning) {
2006-03-10 21:06:26 +03:00
*this->DefaultError << file << ":" << line << " ";
} else {
2006-03-10 21:06:26 +03:00
*this->DefaultOutput << file << ":" << line << " ";
2006-01-03 00:14:21 +03:00
}
}
}
if (error || warning) {
2006-03-10 21:06:26 +03:00
this->DefaultError->write(msg, length);
this->DefaultError->flush();
} else {
2006-03-10 21:06:26 +03:00
this->DefaultOutput->write(msg, length);
this->DefaultOutput->flush();
}
if (msg[length - 1] == '\n' || length > 2) {
2006-03-10 21:06:26 +03:00
this->NewLine = true;
}
if (error) {
cmSystemTools::SetErrorOccured();
}
2006-01-03 00:14:21 +03:00
}