Debian Codegraph @

src/atlas/runtime/trace/Logging.cc

raw
1 /*
2 * (C) Copyright 2013 ECMWF.
3 *
4 * This software is licensed under the terms of the Apache Licence Version 2.0
5 * which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
6 * In applying this licence, ECMWF does not waive the privileges and immunities
7 * granted to it by virtue of its status as an intergovernmental organisation
8 * nor does it submit to any jurisdiction.
9 */
10
11 #include "Logging.h"
12
13 #include <iostream>
14 #include <exception>
15
16 #include "eckit/log/Channel.h"
17
18 #include "atlas/library/Library.h"
19 #include "atlas/parallel/omp/omp.h"
20
21 //-----------------------------------------------------------------------------------------------------------
22
23 namespace atlas {
24 namespace runtime {
25 namespace trace {
26
27 //-----------------------------------------------------------------------------------------------------------
28
29 bool Control::enabled() {
30 return atlas_omp_get_thread_num() == 0;
31 }
32
33 class LoggingState {
34 private:
35 std::ostream* channel_;
36
37 LoggingState() { channel_ = &atlas::Library::instance().traceChannel(); }
38
39 public:
40 static eckit::Channel& empty_channel() {
41 static eckit::Channel channel;
42 return channel;
43 }
44
45 static LoggingState& instance() {
46 static LoggingState channel;
47 return channel;
48 }
49
50 operator std::ostream&() { return *channel_; }
51 operator std::ostream*() { return channel_; }
52
53 void set(std::ostream& channel) { channel_ = &channel; }
54 void set(bool state) {
55 if (state == false) {
56 channel_ = &empty_channel();
57 }
58 }
59 };
60
61 //-----------------------------------------------------------------------------------------------------------
62
63 Logging::Logging(bool state): previous_state_(LoggingState::instance()) {
64 LoggingState::instance().set(state);
65 }
66
67 Logging::Logging(std::ostream& channel): previous_state_(LoggingState::instance()) {
68 LoggingState::instance().set(channel);
69 }
70
71 Logging::~Logging() {
72 LoggingState::instance().set(*previous_state_);
73 }
74
75 std::ostream& Logging::channel() {
76 return LoggingState::instance();
77 }
78
79 bool Logging::enabled() {
80 return LoggingState::instance();
81 }
82
83 void Logging::start(const std::string& title) {
84 if (enabled()) {
85 channel() << title << " ..." << std::endl;
86 }
87 }
88
89 void Logging::stop(const std::string& title, double seconds) {
90 if (enabled()) {
91 if (!std::uncaught_exceptions()){
92 channel() << title << " ... done : " << seconds << "s" << std::endl;
93 }
94 }
95 }
96 //-----------------------------------------------------------------------------------------------------------
97
98 std::ostream& NoLogging::channel() {
99 return LoggingState::empty_channel();
100 }
101
102 //-----------------------------------------------------------------------------------------------------------
103
104 void LoggingResult::stop(const std::string& title, double seconds) {
105 if (enabled()) {
106 channel() << title << " : " << seconds << "s" << std::endl;
107 }
108 }
109
110 //-----------------------------------------------------------------------------------------------------------
111
112 } // namespace trace
113 } // namespace runtime
114 } // namespace atlas