CMake/Source/CPack/cmCPackLog.cxx

226 lines
5.2 KiB
C++
Raw Normal View History

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