summaryrefslogtreecommitdiff
path: root/Source/CPack/cmCPackLog.cxx
blob: 4e8bf0f4e4dfaa9d57e3e955273db5926ce8a816 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
/*============================================================================
  CMake - Cross Platform Makefile Generator
  Copyright 2000-2009 Kitware, Inc., Insight Software Consortium

  Distributed under the OSI-approved BSD License (the "License");
  see accompanying file Copyright.txt for details.

  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.
============================================================================*/

#include "cmCPackLog.h"

#include "cmGeneratedFileStream.h"
#include "cmSystemTools.h"

//----------------------------------------------------------------------
cmCPackLog::cmCPackLog()
{
  this->Verbose = false;
  this->Debug = false;
  this->Quiet = false;
  this->NewLine = true;

  this->LastTag = cmCPackLog::NOTAG;
#undef cerr
#undef cout
  this->DefaultOutput = &std::cout;
  this->DefaultError = &std::cerr;

  this->LogOutput = 0;
  this->LogOutputCleanup = false;
}

//----------------------------------------------------------------------
cmCPackLog::~cmCPackLog()
{
  this->SetLogOutputStream(0);
}

//----------------------------------------------------------------------
void cmCPackLog::SetLogOutputStream(std::ostream* os)
{
  if ( this->LogOutputCleanup && this->LogOutput )
    {
    delete this->LogOutput;
    }
  this->LogOutputCleanup = false;
  this->LogOutput = os;
}

//----------------------------------------------------------------------
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;
    }
  this->LogOutputCleanup = true;
  return true;
}

//----------------------------------------------------------------------
void cmCPackLog::Log(int tag, const char* file, int line,
  const char* msg, size_t length)
{
  // By default no logging
  bool display = false;

  // Display file and line number if debug
  bool useFileAndLine = this->Debug;

  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;
  if ( this->LogOutput && this->LastTag != tag )
    {
    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";
      }
    }
  if ( tag & LOG_DEBUG && this->Debug )
    {
    debug = true;
    display = true;
    if ( needTagString )
      {
      if ( tagString.size() > 0 ) { tagString += ","; }
      tagString = "DEBUG";
      }
    useFileAndLine = true;
    }
  if ( tag & LOG_VERBOSE && this->Verbose )
    {
    verbose = true;
    display = true;
    if ( needTagString )
      {
      if ( tagString.size() > 0 ) { tagString += ","; }
      tagString = "VERBOSE";
      }
    }
  if ( this->Quiet )
    {
    display = false;
    }
  if ( this->LogOutput )
    {
    if ( needTagString )
      {
      *this->LogOutput << "[" << file << ":" << line << " "
        << tagString << "] ";
      }
    this->LogOutput->write(msg, length);
    }
  this->LastTag = tag;
  if ( !display )
    {
    return;
    }
  if ( this->NewLine )
    {
    if ( error && !this->ErrorPrefix.empty() )
      {
      *this->DefaultError << this->ErrorPrefix.c_str();
      }
    else if ( warning && !this->WarningPrefix.empty() )
      {
      *this->DefaultError << this->WarningPrefix.c_str();
      }
    else if ( output && !this->OutputPrefix.empty() )
      {
      *this->DefaultOutput << this->OutputPrefix.c_str();
      }
    else if ( verbose && !this->VerbosePrefix.empty() )
      {
      *this->DefaultOutput << this->VerbosePrefix.c_str();
      }
    else if ( debug && !this->DebugPrefix.empty() )
      {
      *this->DefaultOutput << this->DebugPrefix.c_str();
      }
    else if ( !this->Prefix.empty() )
      {
      *this->DefaultOutput << this->Prefix.c_str();
      }
    if ( useFileAndLine )
      {
      if ( error || warning )
        {
        *this->DefaultError << file << ":" << line << " ";
        }
      else
        {
        *this->DefaultOutput << file << ":" << line << " ";
        }
      }
    }
  if ( error || warning )
    {
    this->DefaultError->write(msg, length);
    this->DefaultError->flush();
    }
  else
    {
    this->DefaultOutput->write(msg, length);
    this->DefaultOutput->flush();
    }
  if ( msg[length-1] == '\n' || length > 2 )
    {
    this->NewLine = true;
    }

  if ( error )
    {
    cmSystemTools::SetErrorOccured();
    }
}