1 /*=========================================================================
3 Program: CMake - Cross-Platform Makefile Generator
4 Module: $RCSfile: cmCPackLog.cxx,v $
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;
30 this->LastTag
= cmCPackLog::NOTAG
;
33 this->DefaultOutput
= &std::cout
;
34 this->DefaultError
= &std::cerr
;
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;
57 //----------------------------------------------------------------------
58 bool cmCPackLog::SetLogOutputFile(const char* fname
)
60 cmGeneratedFileStream
*cg
= 0;
63 cg
= new cmGeneratedFileStream(fname
);
70 this->SetLogOutputStream(cg
);
75 this->LogOutputCleanup
= 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
86 // Display file and line number if debug
87 bool useFileAndLine
= this->Debug
;
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
)
109 if ( tagString
.size() > 0 ) { tagString
+= ","; }
110 tagString
= "VERBOSE";
113 if ( tag
& LOG_WARNING
)
119 if ( tagString
.size() > 0 ) { tagString
+= ","; }
120 tagString
= "WARNING";
123 if ( tag
& LOG_ERROR
)
129 if ( tagString
.size() > 0 ) { tagString
+= ","; }
133 if ( tag
& LOG_DEBUG
&& this->Debug
)
139 if ( tagString
.size() > 0 ) { tagString
+= ","; }
142 useFileAndLine
= true;
144 if ( tag
& LOG_VERBOSE
&& this->Verbose
)
150 if ( tagString
.size() > 0 ) { tagString
+= ","; }
151 tagString
= "VERBOSE";
158 if ( this->LogOutput
)
162 *this->LogOutput
<< "[" << file
<< ":" << line
<< " "
163 << tagString
<< "] ";
165 this->LogOutput
->write(msg
, length
);
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
<< " ";
206 *this->DefaultOutput
<< file
<< ":" << line
<< " ";
210 if ( error
|| warning
)
212 this->DefaultError
->write(msg
, length
);
213 this->DefaultError
->flush();
217 this->DefaultOutput
->write(msg
, length
);
218 this->DefaultOutput
->flush();
220 if ( msg
[length
-1] == '\n' || length
> 2 )
222 this->NewLine
= true;