File: Log.cc

package info (click to toggle)
metview 5.10.2-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 242,296 kB
  • sloc: cpp: 437,117; ansic: 41,433; xml: 19,944; f90: 13,059; sh: 6,562; python: 3,953; yacc: 1,774; lex: 1,121; perl: 701; makefile: 92
file content (141 lines) | stat: -rw-r--r-- 2,623 bytes parent folder | download
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
/***************************** LICENSE START ***********************************

 Copyright 2014 ECMWF and INPE. This software is distributed under the terms
 of the Apache License version 2.0. In applying this license, ECMWF does not
 waive the privileges and immunities granted to it by virtue of its status as
 an Intergovernmental Organization or submit itself to any jurisdiction.

 ***************************** LICENSE END *************************************/

#include "Log.h"

#include "Folder.h"

#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <inc_iostream.h>
#include <time.h>
#include <string.h>


#include "TeeStream.h"
#include "IconObject.h"


// which platform require this one?
// (not SGI, and Linux get compiler errors
//  because of different types...!)
//extern char *sys_errlist[];
//extern const int sys_nerr;

//static std::string logTimeStamp();

Log::Log(IconObject* o) :
    path_(::marstmp()),
    object_(o)
{
}

Log::~Log()
{
    path_.remove();
}

ostream& Log::global()
{
    static ostream* g = 0;
    if (g == 0) {
        g        = &cout;  // Avoid recursion
        string p = Folder::top()->logPath().str();
        g        = new ofstream(p.c_str(), ios::app);
        //Folder::top()->showLog();
    }
    return *g;
}

Log::operator ostream&()
{
    if (tee_.get() == 0) {
        auto_ptr<ostream> o(new ofstream(path_.str().c_str(), ios::app));
        out_ = o;

        auto_ptr<ostream> t(new TeeStream(global(), *out_));
        tee_ = t;
    }

    //Add time stamp
    //*out_ << logTimeStamp();

    global() << "[" << object_->name() << "] ";

    return *tee_;
}

const Path& Log::path()
{
    return path_;
}

ostream& Log::syserr(ostream& s)
{
    int e = errno;

    if (e > 0)
        s << " (" << strerror(e) << ") ";
    else
        s << " (errno = " << e << ") ";
    return s;
}

ostream& Log::error(IconObject* o)
{
    return find(o);
}

ostream& Log::info(IconObject* o)
{
    return find(o);

    //if(o == 0) o = Folder::top();
    //return o->log() << "A";
}

ostream& Log::warning(IconObject* o)
{
    return find(o);
}

ostream& Log::info(const string& p)
{
    return global() << "[" << p << "] ";
}

ostream& Log::warning(const string& p)
{
    return info(p);
}

ostream& Log::error(const string& p)
{
    return info(p);
}

ostream& Log::find(IconObject* o)
{
    if (o == 0)
        o = Folder::top();
    return o->log();
}

/*std::string logTimeStamp()
{  
	time_t t = time(0);   
    	struct tm* now = localtime(&t);	
	char buf[7];
	strftime(buf, sizeof(buf),"%H:%M",now);

	string str;
	str=string(buf);
	return str;
}*/