PSCF v1.1
Log.cpp
1/*
2* Util Package - C++ Utilities for Scientific Computation
3*
4* Copyright 2010 - 2017, The Regents of the University of Minnesota
5* Distributed under the terms of the GNU General Public License.
6*/
7
8#include "Log.h"
9
10namespace Util
11{
12
13 // Define and nullify streamPtr_ and filePtr_
14 std::ostream* Log::streamPtr_ = 0;
15 std::ofstream* Log::filePtr_ = 0;
16
17 /*
18 * This static method exists to guarantee initialization of static
19 * constants that are defined in the same file. Call it somewhere
20 * in the program to guarantee that the contents of this file will
21 * be linked, rather than optimized away. It may only be called once.
22 */
24 {
25 static int nCall = 0;
26 if (nCall == 0) {
27 Log::streamPtr_ = 0;
28 Log::filePtr_ = 0;
29 }
30 ++nCall;
31 }
32
33 /*
34 * Set the log file to a new file.
35 */
36 void Log::setFile(std::ofstream& file)
37 {
38 streamPtr_ = &file;
39 filePtr_ = &file;
40 }
41
42 /*
43 * Close the log file, if any.
44 */
45 void Log::close()
46 {
47 if (filePtr_) {
48 filePtr_->close();
49 }
50 filePtr_ = 0;
51 streamPtr_ = &std::cout;
52 }
53
54 /*
55 * Get the log ostream by reference.
56 */
57 std::ostream& Log::file()
58 {
59 if (!streamPtr_) {
60 streamPtr_ = &std::cout;
61 }
62 return *streamPtr_;
63 }
64
65}
static void setFile(std::ofstream &file)
Set the log ostream to a file.
Definition: Log.cpp:36
static void close()
Close log file, if any.
Definition: Log.cpp:45
static std::ostream & file()
Get log ostream by reference.
Definition: Log.cpp:57
static void initStatic()
Initialize static members.
Definition: Log.cpp:23
Utility classes for scientific computation.
Definition: accumulators.mod:1