~njansson/dolfin/hpc

« back to all changes in this revision

Viewing changes to src/kernel/log/LogStream.cpp

  • Committer: Johannes Ring
  • Date: 2008-03-05 22:43:06 UTC
  • Revision ID: johannr@simula.no-20080305224306-2npsdyhfdpl2esji
The BIG commit!

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
// Copyright (C) 2003-2007 Anders Logg.
2
 
// Licensed under the GNU LGPL Version 2.1.
3
 
//
4
 
// First added:  2003-03-13
5
 
// Last changed: 2007-05-15
6
 
 
7
 
#include <stdio.h>
8
 
#include <cmath>
9
 
#include <string>
10
 
#include <dolfin/constants.h>
11
 
#include <dolfin/log.h>
12
 
#include <dolfin/LogManager.h>
13
 
#include <dolfin/LogStream.h>
14
 
 
15
 
using namespace dolfin;
16
 
 
17
 
// Definition of the global cout and endl variables
18
 
LogStream dolfin::cout(LogStream::COUT);
19
 
LogStream dolfin::endl(LogStream::ENDL);
20
 
 
21
 
//-----------------------------------------------------------------------------
22
 
LogStream::LogStream(Type type)
23
 
{
24
 
  this->type = type;
25
 
  buffer = new char[DOLFIN_LINELENGTH];
26
 
  current = 0;
27
 
}
28
 
//-----------------------------------------------------------------------------
29
 
LogStream::~LogStream()
30
 
{
31
 
  if (buffer)
32
 
    delete [] buffer;
33
 
}
34
 
//-----------------------------------------------------------------------------
35
 
LogStream& LogStream::operator<<(const char* s)
36
 
{
37
 
  add(s);
38
 
  return *this;
39
 
}
40
 
//-----------------------------------------------------------------------------
41
 
LogStream& LogStream::operator<<(const std::string& s)
42
 
{
43
 
  add(s.c_str());
44
 
  return *this;
45
 
}
46
 
//-----------------------------------------------------------------------------
47
 
LogStream& LogStream::operator<<(const LogStream& stream)
48
 
{
49
 
  if (stream.type == ENDL)
50
 
  {
51
 
    LogManager::logger.message(buffer);
52
 
    current = 0;
53
 
    buffer[0] = '\0';
54
 
  }
55
 
  else
56
 
    add(stream.buffer);
57
 
  
58
 
  return *this;
59
 
}
60
 
//-----------------------------------------------------------------------------
61
 
LogStream& LogStream::operator<<(int a)
62
 
{
63
 
  char tmp[DOLFIN_LINELENGTH];
64
 
  snprintf(tmp, DOLFIN_LINELENGTH, "%d", a);
65
 
  add(tmp);
66
 
  return *this;
67
 
}
68
 
//-----------------------------------------------------------------------------
69
 
LogStream& LogStream::operator<<(unsigned int a)
70
 
{
71
 
  char tmp[DOLFIN_LINELENGTH];
72
 
  snprintf(tmp, DOLFIN_LINELENGTH, "%u", a);
73
 
  add(tmp);
74
 
  return *this;
75
 
}
76
 
//-----------------------------------------------------------------------------
77
 
LogStream& LogStream::operator<<(real a)
78
 
{
79
 
  char tmp[DOLFIN_LINELENGTH];
80
 
  /*
81
 
    if (fabs(a) < 1e-5 || fabs(a) > 1e5)
82
 
    sprintf(tmp, "%e", a);
83
 
    else
84
 
    sprintf(tmp, "%f", a);
85
 
  */
86
 
  snprintf(tmp, DOLFIN_LINELENGTH, "%.3g", a);
87
 
  add(tmp);
88
 
  return *this;
89
 
}
90
 
//-----------------------------------------------------------------------------
91
 
LogStream& LogStream::operator<<(complex z)
92
 
{
93
 
  char tmp[DOLFIN_LINELENGTH];
94
 
  snprintf(tmp, DOLFIN_LINELENGTH, "%f + %fi", z.real(), z.imag());
95
 
  add(tmp);
96
 
  return *this;
97
 
}
98
 
//-----------------------------------------------------------------------------
99
 
void LogStream::disp() const
100
 
{
101
 
  // This is used for debugging
102
 
 
103
 
  printf("This i a LogStream of type ");
104
 
  switch ( type ) {
105
 
  case COUT:
106
 
    printf("cout.\n");
107
 
    break;
108
 
  default:
109
 
    printf("endl.\n");
110
 
  }
111
 
 
112
 
  printf("The buffer size is %d. Currently at position %d. \n",
113
 
         DOLFIN_LINELENGTH, current);
114
 
}
115
 
//-----------------------------------------------------------------------------
116
 
void LogStream::add(const char* msg)
117
 
{
118
 
  for (int i = 0; msg[i]; i++)
119
 
  {
120
 
    if (current >= (DOLFIN_LINELENGTH-1))
121
 
    {
122
 
      LogManager::logger.message(buffer);
123
 
      current = 0;
124
 
      buffer[0] = '\0';
125
 
      return;
126
 
    }
127
 
    buffer[current++] = msg[i];
128
 
  }
129
 
  buffer[current] = '\0';
130
 
}
131
 
//-----------------------------------------------------------------------------