Resync.
[CMakeLuaTailorHgBridge.git] / CMakeLua / Source / CPack / cmCPackLog.cxx
blobabc80d0cfa1568ef10a73e24c76c4a3abee34890
1 /*=========================================================================
3 Program: CMake - Cross-Platform Makefile Generator
4 Module: $RCSfile: cmCPackLog.cxx,v $
5 Language: C++
6 Date: $Date: 2006/03/10 18:06:26 $
7 Version: $Revision: 1.7 $
9 Copyright (c) 2002 Kitware, Inc., Insight Consortium. All rights reserved.
10 See Copyright.txt or http://www.cmake.org/HTML/Copyright.html for details.
12 This software is distributed WITHOUT ANY WARRANTY; without even
13 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 PURPOSE. See the above copyright notices for more information.
16 =========================================================================*/
18 #include "cmCPackLog.h"
20 #include "cmGeneratedFileStream.h"
22 //----------------------------------------------------------------------
23 cmCPackLog::cmCPackLog()
25 this->Verbose = false;
26 this->Debug = false;
27 this->Quiet = false;
28 this->NewLine = true;
30 this->LastTag = cmCPackLog::NOTAG;
31 #undef cerr
32 #undef cout
33 this->DefaultOutput = &std::cout;
34 this->DefaultError = &std::cerr;
36 this->LogOutput = 0;
37 this->LogOutputCleanup = false;
40 //----------------------------------------------------------------------
41 cmCPackLog::~cmCPackLog()
43 this->SetLogOutputStream(0);
46 //----------------------------------------------------------------------
47 void cmCPackLog::SetLogOutputStream(std::ostream* os)
49 if ( this->LogOutputCleanup && this->LogOutput )
51 delete this->LogOutput;
53 this->LogOutputCleanup = false;
54 this->LogOutput = os;
57 //----------------------------------------------------------------------
58 bool cmCPackLog::SetLogOutputFile(const char* fname)
60 cmGeneratedFileStream *cg = 0;
61 if ( fname )
63 cg = new cmGeneratedFileStream(fname);
65 if ( cg && !*cg )
67 delete cg;
68 cg = 0;
70 this->SetLogOutputStream(cg);
71 if ( !cg )
73 return false;
75 this->LogOutputCleanup = true;
76 return true;
79 //----------------------------------------------------------------------
80 void cmCPackLog::Log(int tag, const char* file, int line,
81 const char* msg, size_t length)
83 // By default no logging
84 bool display = false;
86 // Display file and line number if debug
87 bool useFileAndLine = this->Debug;
89 bool output = false;
90 bool debug = false;
91 bool warning = false;
92 bool error = false;
93 bool verbose = false;
95 // When writing in file, add list of tags whenever tag changes.
96 std::string tagString;
97 bool needTagString = false;
98 if ( this->LogOutput && this->LastTag != tag )
100 needTagString = true;
103 if ( tag & LOG_OUTPUT )
105 output = true;
106 display = true;
107 if ( needTagString )
109 if ( tagString.size() > 0 ) { tagString += ","; }
110 tagString = "VERBOSE";
113 if ( tag & LOG_WARNING )
115 warning = true;
116 display = true;
117 if ( needTagString )
119 if ( tagString.size() > 0 ) { tagString += ","; }
120 tagString = "WARNING";
123 if ( tag & LOG_ERROR )
125 error = true;
126 display = true;
127 if ( needTagString )
129 if ( tagString.size() > 0 ) { tagString += ","; }
130 tagString = "ERROR";
133 if ( tag & LOG_DEBUG && this->Debug )
135 debug = true;
136 display = true;
137 if ( needTagString )
139 if ( tagString.size() > 0 ) { tagString += ","; }
140 tagString = "DEBUG";
142 useFileAndLine = true;
144 if ( tag & LOG_VERBOSE && this->Verbose )
146 verbose = true;
147 display = true;
148 if ( needTagString )
150 if ( tagString.size() > 0 ) { tagString += ","; }
151 tagString = "VERBOSE";
154 if ( this->Quiet )
156 display = false;
158 if ( this->LogOutput )
160 if ( needTagString )
162 *this->LogOutput << "[" << file << ":" << line << " "
163 << tagString << "] ";
165 this->LogOutput->write(msg, length);
167 this->LastTag = tag;
168 if ( !display )
170 return;
172 if ( this->NewLine )
174 if ( error && !this->ErrorPrefix.empty() )
176 *this->DefaultError << this->ErrorPrefix.c_str();
178 else if ( warning && !this->WarningPrefix.empty() )
180 *this->DefaultError << this->WarningPrefix.c_str();
182 else if ( output && !this->OutputPrefix.empty() )
184 *this->DefaultOutput << this->OutputPrefix.c_str();
186 else if ( verbose && !this->VerbosePrefix.empty() )
188 *this->DefaultOutput << this->VerbosePrefix.c_str();
190 else if ( debug && !this->DebugPrefix.empty() )
192 *this->DefaultOutput << this->DebugPrefix.c_str();
194 else if ( !this->Prefix.empty() )
196 *this->DefaultOutput << this->Prefix.c_str();
198 if ( useFileAndLine )
200 if ( error || warning )
202 *this->DefaultError << file << ":" << line << " ";
204 else
206 *this->DefaultOutput << file << ":" << line << " ";
210 if ( error || warning )
212 this->DefaultError->write(msg, length);
213 this->DefaultError->flush();
215 else
217 this->DefaultOutput->write(msg, length);
218 this->DefaultOutput->flush();
220 if ( msg[length-1] == '\n' || length > 2 )
222 this->NewLine = true;