File: IpJournalist.cpp

package info (click to toggle)
coinor-ipopt 3.11.9-2.1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 24,616 kB
  • sloc: cpp: 75,373; sh: 11,170; ansic: 2,798; makefile: 2,247; java: 632; fortran: 342
file content (474 lines) | stat: -rw-r--r-- 12,127 bytes parent folder | download | duplicates (5)
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
// Copyright (C) 2004, 2009 International Business Machines and others.
// All Rights Reserved.
// This code is published under the Eclipse Public License.
//
// $Id: IpJournalist.cpp 1861 2010-12-21 21:34:47Z andreasw $
//
// Authors:  Carl Laird, Andreas Waechter     IBM    2004-08-13

#include "IpoptConfig.h"
#include "IpJournalist.hpp"
#include "IpDebug.hpp"

#ifdef HAVE_CSTDIO
# include <cstdio>
#else
# ifdef HAVE_STDIO_H
#  include <stdio.h>
# else
#  error "don't have header file for stdio"
# endif
#endif

#ifdef HAVE_CSTRING
# include <cstring>
#else
# ifdef HAVE_STRING_H
#  include <string.h>
# else
#  error "don't have header file for string"
# endif
#endif

namespace Ipopt
{

  Journalist::Journalist()
  {}

  Journalist::~Journalist()
  {
    journals_.clear();
  }

  void Journalist::Printf( EJournalLevel level, EJournalCategory category,
                           const char* pformat, ... ) const
  {
    // wrap the arguments and pass to VPrintf
    va_list ap;
    va_start(ap, pformat);

    VPrintf(level, category, pformat, ap);

    va_end(ap);
  }

  void Journalist::PrintStringOverLines(EJournalLevel level,
                                        EJournalCategory category,
                                        Index indent_spaces, Index max_length,
                                        const std::string& line) const
  {
    DBG_ASSERT(indent_spaces + max_length + 1 < 1024);
    char buffer[1024];
    std::string::size_type last_line_pos = 0;
    std::string::size_type last_word_pos = 0;
    bool first_line = true;
    Index buffer_pos = 0;

    while (last_line_pos < line.length()) {
      std::string::size_type line_pos = last_line_pos;
      Index curr_length = 0;
      while (curr_length < max_length && line_pos < line.length()) {
        buffer[buffer_pos] = line[line_pos];
        if (line[line_pos] == ' ') {
          last_word_pos = line_pos+1;
        }
        curr_length++;
        buffer_pos++;
        line_pos++;
      }
      if (line_pos == line.length()) {
        // This is the last line to be printed.
        buffer[buffer_pos] = '\0';
        Printf(level, category, "%s", buffer);
        break;
      }
      if (last_word_pos == last_line_pos) {
        if (line[line_pos]==' ') {
          buffer[buffer_pos] = '\0';
          last_word_pos = line_pos+1;
          last_line_pos = line_pos+1;
        }
        else {
          // The current word is too long to fit into one line
          // split word over two lines
          buffer[buffer_pos-1] = '-';
          buffer[buffer_pos] = '\0';
          last_word_pos = line_pos-1;
          last_line_pos = last_word_pos;
        }
      }
      else {
        // insert '\0' character after last complete word
        buffer[buffer_pos-(line_pos-last_word_pos)-1] = '\0';
        last_line_pos = last_word_pos;
      }

      Printf(level, category, "%s\n", buffer);
      if (first_line) {
        for (Index i=0; i<indent_spaces; i++) {
          buffer[i] = ' ';
        }
        first_line = false;
      }
      buffer_pos = indent_spaces;
    }
  }

  void Journalist::PrintfIndented( EJournalLevel level,
                                   EJournalCategory category, Index indent_level,
                                   const char* pformat, ... ) const
  {
    // wrap the arguments and pass to VPrintfIndented
    va_list ap;
    va_start(ap, pformat);

    VPrintfIndented(level, category, indent_level, pformat, ap);

    va_end(ap);
  }

  //   void Journalist::PrintVector(EJournalLevel level,
  //                                EJournalCategory category,
  //                                const std::string& name,
  //                                const Vector& vector,
  //                                Index indent,
  //                                const std::string prefix) const
  //   {
  //     // print the msg on every journal that accepts
  //     // the category and output level
  //     for (Index i=0; i<(Index)journals_.size(); i++) {
  //       if (journals_[i]->IsAccepted(category, level)) {
  //         // print the message
  //         journals_[i]->PrintVector(name, vector, indent, prefix);
  //       }
  //     }
  //   }

  //   void Journalist::PrintMatrix(EJournalLevel level,
  //                                EJournalCategory category,
  //                                const std::string& name,
  //                                const Matrix& matrix,
  //                                Index indent /*=0*/,
  //                                std::string prefix /*=""*/) const
  //   {
  //     // print the msg on every journal that accepts
  //     // the category and output level
  //     for (Index i=0; i<(Index)journals_.size(); i++) {
  //       if (journals_[i]->IsAccepted(category, level)) {
  //         // print the message
  //         journals_[i]->PrintMatrix(name, matrix, indent, prefix);
  //       }
  //     }
  //   }

  void Journalist::VPrintf(
    EJournalLevel level,
    EJournalCategory category,
    const char* pformat, va_list ap) const
  {
    // print the msg on every journal that accepts
    // the category and output level
    for (Index i=0; i<(Index)journals_.size(); i++) {
      if (journals_[i]->IsAccepted(category, level)) {
        // print the message
#ifdef HAVE_VA_COPY
        va_list apcopy;
        va_copy(apcopy, ap);
        journals_[i]->Printf(category, level, pformat, apcopy);
        va_end(apcopy);
#else

        journals_[i]->Printf(category, level, pformat, ap);
#endif

      }
    }
  }

  void Journalist::VPrintfIndented(
    EJournalLevel level,
    EJournalCategory category,
    Index indent_level,
    const char* pformat, va_list ap) const
  {
    // print the msg on every journal that accepts
    // the category and output level
    for (Index i=0; i<(Index)journals_.size(); i++) {
      if (journals_[i]->IsAccepted(category, level)) {

        // indent the appropriate amount
        for (Index s=0; s<indent_level; s++) {
          journals_[i]->Print(category, level, "  ");
        }

        // print the message
#ifdef HAVE_VA_COPY
        va_list apcopy;
        va_copy(apcopy, ap);
        journals_[i]->Printf(category, level, pformat, apcopy);
        va_end(apcopy);
#else

        journals_[i]->Printf(category, level, pformat, ap);
#endif

      }
    }
  }

  bool Journalist::ProduceOutput(EJournalLevel level,
                                 EJournalCategory category) const
  {
    for (Index i=0; i<(Index)journals_.size(); i++) {
      if (journals_[i]->IsAccepted(category, level)) {
        return true;
      }
    }
    return false;
  }

  bool Journalist::AddJournal(const SmartPtr<Journal> jrnl)
  {
    DBG_ASSERT(IsValid(jrnl));
    std::string name = jrnl->Name();

    SmartPtr<Journal> temp = GetJournal(name);
    DBG_ASSERT(IsNull(temp));
    if (IsValid(temp)) {
      return false;
    }

    journals_.push_back(jrnl);
    return true;
  }

  SmartPtr<Journal> Journalist::AddFileJournal(
    const std::string& journal_name,
    const std::string& fname,
    EJournalLevel default_level
  )
  {
    SmartPtr<FileJournal> temp = new FileJournal(journal_name, default_level);

    // Open the file (Note:, a fname of "stdout" is handled by the
    // Journal class to mean stdout, etc.
    if (temp->Open(fname.c_str()) && AddJournal(GetRawPtr(temp))) {
      return GetRawPtr(temp);
    }
    return NULL;
  }

  void Journalist::FlushBuffer() const
  {
    for (Index i=0; i<(Index)journals_.size(); i++) {
      journals_[i]->FlushBuffer();
    }
  }

  SmartPtr<Journal> Journalist::GetJournal(
    const std::string& journal_name
  )
  {
    SmartPtr<Journal> retValue = NULL;

    // try to find the journal
    for (Index i=0; i<(Index)journals_.size(); i++) {
      SmartPtr<Journal> tmp = journals_[i];
      if (tmp->Name() == journal_name) {
        retValue = tmp;
        break;
      }
    }

    return retValue;
  }

  void Journalist::DeleteAllJournals()
  {
    for (Index i=0; i<(Index)journals_.size(); i++) {
      journals_[i]=NULL;
    }
    journals_.resize(0);
  }

  ///////////////////////////////////////////////////////////////////////////
  //                 Implementation of the Journal class                   //
  ///////////////////////////////////////////////////////////////////////////

  Journal::Journal(
    const std::string& name,
    EJournalLevel default_level
  )
      :
      name_(name)
  {
    for (Index i=0; i<J_LAST_CATEGORY; i++) {
      print_levels_[i] = default_level;
    }
  }

  Journal::~Journal()
  {}

  std::string Journal::Name()
  {
    return name_;
  }

  bool Journal::IsAccepted(
    EJournalCategory category,
    EJournalLevel level
  ) const
  {
    if (print_levels_[(Index)category] >= (Index) level) {
      return true;
    }

    return false;
  }

  void Journal::SetPrintLevel(
    EJournalCategory category,
    EJournalLevel level)
  {
    print_levels_[(Index)category] = (Index) level;
  }

  void Journal::SetAllPrintLevels(
    EJournalLevel level)
  {
    for (Index category=(Index)J_DBG;
         category<(Index)J_USER_APPLICATION;
         category++) {
      print_levels_[category] = (Index) level;
    }
  }


  ///////////////////////////////////////////////////////////////////////////
  //                 Implementation of the FileJournal class               //
  ///////////////////////////////////////////////////////////////////////////

  FileJournal::FileJournal(
    const std::string& name,
    EJournalLevel default_level
  )
      :
      Journal(name, default_level),
      file_(NULL)
  {}

  FileJournal::~FileJournal()
  {
    if (file_ && file_ != stdout && file_ != stderr) {
      // close the file
      fclose(file_);
    }
    file_ = NULL;
  }


  bool FileJournal::Open(const char* fname)
  {
    if (file_ && file_ != stdout && file_ != stderr) {
      // file already opened, close it
      fclose(file_);
    }
    file_ = NULL;

    if (strcmp("stdout", fname)==0) {
      file_=stdout;
      return true;
    }
    else if (strcmp("stderr", fname)==0) {
      file_=stderr;
      return true;
    }
    else {
      // open the file on disk
      file_ = fopen(fname, "w+");
      if (file_) {
        return true;
      }
    }

    return false;
  }


  void FileJournal::PrintImpl(EJournalCategory category, EJournalLevel level,
                              const char* str)
  {
    DBG_START_METH("Journal::Print", 0);
    if (file_) {
      fprintf(file_, "%s", str);
      DBG_EXEC(0, fflush(file_));
    }
  }

  void FileJournal::PrintfImpl(EJournalCategory category, EJournalLevel level,
                               const char* pformat, va_list ap)
  {
    DBG_START_METH("Journal::Printf", 0);
    if (file_) {
      vfprintf(file_, pformat, ap);
      DBG_EXEC(0, fflush(file_));
    }
  }

  void FileJournal::FlushBufferImpl()
  {
    if (file_) {
      fflush(file_);
    }
  }


  ///////////////////////////////////////////////////////////////////////////
  //                 Implementation of the StreamJournal class               //
  ///////////////////////////////////////////////////////////////////////////

  StreamJournal::StreamJournal(
    const std::string& name,
    EJournalLevel default_level
  )
      :
      Journal(name, default_level),
      os_(NULL)
  {}

  void StreamJournal::SetOutputStream(std::ostream* os)
  {
    os_ = os;
  }

  void StreamJournal::PrintImpl(EJournalCategory category, EJournalLevel level,
                                const char* str)
  {
    DBG_START_METH("StreamJournal::PrintImpl", 0);
    if (os_) {
      *os_ << str;
      DBG_EXEC(0, *os_ << std::flush);
    }
  }

  void StreamJournal::PrintfImpl(EJournalCategory category, EJournalLevel level,
                                 const char* pformat, va_list ap)
  {
    DBG_START_METH("StreamJournal::PrintfImpl", 0);
    if (os_) {
      vsprintf(buffer_, pformat, ap);
      *os_ << buffer_;
      DBG_EXEC(0, *os_ << std::flush);
    }
  }

  void StreamJournal::FlushBufferImpl()
  {
    if (os_) {
      *os_ << std::flush;
    }
  }

} // namespace Ipopt