Resync
[CMakeLuaTailorHgBridge.git] / CMakeLua / Source / CPack / cmCPackLog.cxx
blob30818e997e945c5509e19329796143547f2768e7
1 /*=========================================================================
3 Program: CMake - Cross-Platform Makefile Generator
4 Module: $RCSfile: cmCPackLog.cxx,v $
5 Language: C++
6 <<<<<<< cmCPackLog.cxx
7 Date: $Date: 2006/03/10 18:06:26 $
8 Version: $Revision: 1.7 $
9 =======
10 Date: $Date: 2009-01-22 18:56:13 $
11 Version: $Revision: 1.8 $
12 >>>>>>> 1.8
14 Copyright (c) 2002 Kitware, Inc., Insight Consortium. All rights reserved.
15 See Copyright.txt or http://www.cmake.org/HTML/Copyright.html for details.
17 This software is distributed WITHOUT ANY WARRANTY; without even
18 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
19 PURPOSE. See the above copyright notices for more information.
21 =========================================================================*/
23 #include "cmCPackLog.h"
25 #include "cmGeneratedFileStream.h"
26 #include "cmSystemTools.h"
28 //----------------------------------------------------------------------
29 cmCPackLog::cmCPackLog()
31 this->Verbose = false;
32 this->Debug = false;
33 this->Quiet = false;
34 this->NewLine = true;
36 this->LastTag = cmCPackLog::NOTAG;
37 #undef cerr
38 #undef cout
39 this->DefaultOutput = &std::cout;
40 this->DefaultError = &std::cerr;
42 this->LogOutput = 0;
43 this->LogOutputCleanup = false;
46 //----------------------------------------------------------------------
47 cmCPackLog::~cmCPackLog()
49 this->SetLogOutputStream(0);
52 //----------------------------------------------------------------------
53 void cmCPackLog::SetLogOutputStream(std::ostream* os)
55 if ( this->LogOutputCleanup && this->LogOutput )
57 delete this->LogOutput;
59 this->LogOutputCleanup = false;
60 this->LogOutput = os;
63 //----------------------------------------------------------------------
64 bool cmCPackLog::SetLogOutputFile(const char* fname)
66 cmGeneratedFileStream *cg = 0;
67 if ( fname )
69 cg = new cmGeneratedFileStream(fname);
71 if ( cg && !*cg )
73 delete cg;
74 cg = 0;
76 this->SetLogOutputStream(cg);
77 if ( !cg )
79 return false;
81 this->LogOutputCleanup = true;
82 return true;
85 //----------------------------------------------------------------------
86 void cmCPackLog::Log(int tag, const char* file, int line,
87 const char* msg, size_t length)
89 // By default no logging
90 bool display = false;
92 // Display file and line number if debug
93 bool useFileAndLine = this->Debug;
95 bool output = false;
96 bool debug = false;
97 bool warning = false;
98 bool error = false;
99 bool verbose = false;
101 // When writing in file, add list of tags whenever tag changes.
102 std::string tagString;
103 bool needTagString = false;
104 if ( this->LogOutput && this->LastTag != tag )
106 needTagString = true;
109 if ( tag & LOG_OUTPUT )
111 output = true;
112 display = true;
113 if ( needTagString )
115 if ( tagString.size() > 0 ) { tagString += ","; }
116 tagString = "VERBOSE";
119 if ( tag & LOG_WARNING )
121 warning = true;
122 display = true;
123 if ( needTagString )
125 if ( tagString.size() > 0 ) { tagString += ","; }
126 tagString = "WARNING";
129 if ( tag & LOG_ERROR )
131 error = true;
132 display = true;
133 if ( needTagString )
135 if ( tagString.size() > 0 ) { tagString += ","; }
136 tagString = "ERROR";
139 if ( tag & LOG_DEBUG && this->Debug )
141 debug = true;
142 display = true;
143 if ( needTagString )
145 if ( tagString.size() > 0 ) { tagString += ","; }
146 tagString = "DEBUG";
148 useFileAndLine = true;
150 if ( tag & LOG_VERBOSE && this->Verbose )
152 verbose = true;
153 display = true;
154 if ( needTagString )
156 if ( tagString.size() > 0 ) { tagString += ","; }
157 tagString = "VERBOSE";
160 if ( this->Quiet )
162 display = false;
164 if ( this->LogOutput )
166 if ( needTagString )
168 *this->LogOutput << "[" << file << ":" << line << " "
169 << tagString << "] ";
171 this->LogOutput->write(msg, length);
173 this->LastTag = tag;
174 if ( !display )
176 return;
178 if ( this->NewLine )
180 if ( error && !this->ErrorPrefix.empty() )
182 *this->DefaultError << this->ErrorPrefix.c_str();
184 else if ( warning && !this->WarningPrefix.empty() )
186 *this->DefaultError << this->WarningPrefix.c_str();
188 else if ( output && !this->OutputPrefix.empty() )
190 *this->DefaultOutput << this->OutputPrefix.c_str();
192 else if ( verbose && !this->VerbosePrefix.empty() )
194 *this->DefaultOutput << this->VerbosePrefix.c_str();
196 else if ( debug && !this->DebugPrefix.empty() )
198 *this->DefaultOutput << this->DebugPrefix.c_str();
200 else if ( !this->Prefix.empty() )
202 *this->DefaultOutput << this->Prefix.c_str();
204 if ( useFileAndLine )
206 if ( error || warning )
208 *this->DefaultError << file << ":" << line << " ";
210 else
212 *this->DefaultOutput << file << ":" << line << " ";
216 if ( error || warning )
218 this->DefaultError->write(msg, length);
219 this->DefaultError->flush();
221 else
223 this->DefaultOutput->write(msg, length);
224 this->DefaultOutput->flush();
226 if ( msg[length-1] == '\n' || length > 2 )
228 this->NewLine = true;
231 if ( error )
233 cmSystemTools::SetErrorOccured();