1 /*=========================================================================
3 Program: CMake - Cross-Platform Makefile Generator
4 Module: $RCSfile: cmCPackLog.cxx,v $
7 Date: $Date: 2006/03/10 18:06:26 $
8 Version: $Revision: 1.7 $
10 Date: $Date: 2009-01-22 18:56:13 $
11 Version: $Revision: 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;
36 this->LastTag
= cmCPackLog::NOTAG
;
39 this->DefaultOutput
= &std::cout
;
40 this->DefaultError
= &std::cerr
;
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;
63 //----------------------------------------------------------------------
64 bool cmCPackLog::SetLogOutputFile(const char* fname
)
66 cmGeneratedFileStream
*cg
= 0;
69 cg
= new cmGeneratedFileStream(fname
);
76 this->SetLogOutputStream(cg
);
81 this->LogOutputCleanup
= 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
92 // Display file and line number if debug
93 bool useFileAndLine
= this->Debug
;
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
)
115 if ( tagString
.size() > 0 ) { tagString
+= ","; }
116 tagString
= "VERBOSE";
119 if ( tag
& LOG_WARNING
)
125 if ( tagString
.size() > 0 ) { tagString
+= ","; }
126 tagString
= "WARNING";
129 if ( tag
& LOG_ERROR
)
135 if ( tagString
.size() > 0 ) { tagString
+= ","; }
139 if ( tag
& LOG_DEBUG
&& this->Debug
)
145 if ( tagString
.size() > 0 ) { tagString
+= ","; }
148 useFileAndLine
= true;
150 if ( tag
& LOG_VERBOSE
&& this->Verbose
)
156 if ( tagString
.size() > 0 ) { tagString
+= ","; }
157 tagString
= "VERBOSE";
164 if ( this->LogOutput
)
168 *this->LogOutput
<< "[" << file
<< ":" << line
<< " "
169 << tagString
<< "] ";
171 this->LogOutput
->write(msg
, length
);
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
<< " ";
212 *this->DefaultOutput
<< file
<< ":" << line
<< " ";
216 if ( error
|| warning
)
218 this->DefaultError
->write(msg
, length
);
219 this->DefaultError
->flush();
223 this->DefaultOutput
->write(msg
, length
);
224 this->DefaultOutput
->flush();
226 if ( msg
[length
-1] == '\n' || length
> 2 )
228 this->NewLine
= true;
233 cmSystemTools::SetErrorOccured();