1 /*
  2  *   Copyright (c) 2010, Michael Lehn
  3  *
  4  *   All rights reserved.
  5  *
  6  *   Redistribution and use in source and binary forms, with or without
  7  *   modification, are permitted provided that the following conditions
  8  *   are met:
  9  *
 10  *   1) Redistributions of source code must retain the above copyright
 11  *      notice, this list of conditions and the following disclaimer.
 12  *   2) Redistributions in binary form must reproduce the above copyright
 13  *      notice, this list of conditions and the following disclaimer in
 14  *      the documentation and/or other materials provided with the
 15  *      distribution.
 16  *   3) Neither the name of the FLENS development group nor the names of
 17  *      its contributors may be used to endorse or promote products derived
 18  *      from this software without specific prior written permission.
 19  *
 20  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 21  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 22  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 23  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 24  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 25  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 26  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 27  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 28  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 29  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 30  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 31  */
 32 
 33 #include <algorithm>
 34 #include <string>
 35 #include <flens/debug/aux/closurelog.h>
 36 #include <flens/debug/aux/closurelogstream.h>
 37 #include <flens/aux/macros.h>
 38 #include <iostream>
 39 
 40 namespace flens { namespace verbose {
 41 
 42 bool
 43 ClosureLog::_started = false;
 44 
 45 int
 46 ClosureLog::_indentLevel = -1;
 47 
 48 std::string
 49 ClosureLog::_tag;
 50 
 51 std::ofstream
 52 ClosureLog::_out;
 53 
 54 VariablePool
 55 ClosureLog::variablePool;
 56 
 57 ClosureLogStream
 58 ClosureLog::_closureLogStream = ClosureLogStream(ClosureLog::variablePool,
 59                                                  ClosureLog::_out);
 60 
 61 void
 62 ClosureLog::start(const char *filename, bool clearLog)
 63 {
 64     std::ios_base::openmode mode = (clearLog) ? std::ios_base::trunc
 65                                               : std::ios_base::app;
 66     _out.open(filename, mode | std::ios_base::out);
 67     _started = true;
 68     _indentLevel = 0;
 69     _tag.assign("");
 70 
 71 }
 72 
 73 void
 74 ClosureLog::stop()
 75 {
 76     _out << std::endl;
 77     _out.close();
 78     _started = false;
 79 }
 80 
 81 bool
 82 ClosureLog::started()
 83 {
 84     return _started;
 85 }
 86 
 87 void
 88 ClosureLog::separator()
 89 {
 90     if (_started && _indentLevel==1) {
 91         std::string sep(80'-');
 92         _out << std::endl << sep << std::endl;
 93     }
 94 }
 95 
 96 bool
 97 ClosureLog::openEntry()
 98 {
 99     if (_started) {
100         return true;
101     }
102     return false;
103 }
104 
105 bool
106 ClosureLog::createEntry()
107 {
108     if (_started) {
109         ++_indentLevel;
110         return true;
111     }
112     return false;
113 }
114 
115 void
116 ClosureLog::closeEntry()
117 {
118     if (_started) {
119         --_indentLevel;
120     }
121 }
122 
123 
124 void
125 ClosureLog::setTag(const char *tag)
126 {
127     _tag.assign(tag);
128 }
129 
130 void
131 ClosureLog::unsetTag()
132 {
133     _tag.assign("");
134 }
135 
136 ClosureLogStream &
137 ClosureLog::append(bool startNewLine)
138 {
139     ASSERT(_started);
140 
141 
142     if (startNewLine) {
143         std::string indent(std::max((_indentLevel-1)*40), ' ');
144         _out << std::endl;
145         _out << indent << _tag;
146     }
147     return _closureLogStream;
148 }
149 
150 } } // namespace verbose, namespace flens