Field3D
Log.cpp
Go to the documentation of this file.
1 //----------------------------------------------------------------------------//
2 
3 /*
4  * Copyright (c) 2009 Sony Pictures Imageworks Inc
5  *
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  * Redistributions in binary form must reproduce the above copyright
15  * notice, this list of conditions and the following disclaimer in the
16  * documentation and/or other materials provided with the
17  * distribution. Neither the name of Sony Pictures Imageworks nor the
18  * names of its contributors may be used to endorse or promote
19  * products derived from this software without specific prior written
20  * permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
28  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
29  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
31  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
33  * OF THE POSSIBILITY OF SUCH DAMAGE.
34  */
35 
36 //----------------------------------------------------------------------------//
37 
42 //----------------------------------------------------------------------------//
43 
44 #include <unistd.h>
45 #include <ios>
46 #include <fstream>
47 
48 #include <iostream>
49 
50 #include "Log.h"
51 
52 //----------------------------------------------------------------------------//
53 
54 using namespace std;
55 
56 //----------------------------------------------------------------------------//
57 
59 
60 //----------------------------------------------------------------------------//
61 
62 namespace Msg {
63 
64 //----------------------------------------------------------------------------//
65 
66 static int g_verbosity = 1;
67 
68 //----------------------------------------------------------------------------//
69 
70 void print(Severity severity, const std::string &message)
71 {
72  if (g_verbosity < 1)
73  return;
74 
75  switch(severity) {
76  case SevWarning:
77  cout << "WARNING: ";
78  break;
79  case SevMessage:
80  default:
81  break;
82  // Do nothing
83  }
84 
85  cout << message << endl;
86 }
87 
88 //----------------------------------------------------------------------------//
89 
90 void setVerbosity (int level)
91 {
92  g_verbosity = level;
93 }
94 
95 //----------------------------------------------------------------------------//
96 
97 } // namespace Log
98 
99 //----------------------------------------------------------------------------//
100 
101 std::string bytesToString(int64_t bytes)
102 {
103  using std::stringstream;
104 
105  stringstream ss;
106  ss.precision(3);
107  ss.setf(std::ios::fixed, std:: ios::floatfield);
108 
109  // Make it work for negative numbers
110  if (bytes < 0) {
111  ss << "-";
112  bytes = -bytes;
113  }
114 
115  if (bytes < 1024) {
116  // Bytes
117  ss << bytes << " B";
118  return ss.str();
119  } else if (bytes < (1024 * 1024)) {
120  // Kilobytes
121  ss << bytes / static_cast<float>(1024) << " KB";
122  return ss.str();
123  } else if (bytes < (1024 * 1024 * 1024)) {
124  // Megabytes
125  ss << bytes / static_cast<float>(1024 * 1024) << " MB";
126  return ss.str();
127  } else {
128  // Gigabytes
129  ss << bytes / static_cast<float>(1024 * 1024 * 1024) << " GB";
130  return ss.str();
131  }
132 }
133 
134 //----------------------------------------------------------------------------//
135 
136 size_t currentRSS()
137 {
139 
140 #ifdef __linux__
141 
142  using std::ios_base;
143  using std::ifstream;
144  using std::string;
145  ifstream stat_stream("/proc/self/stat", ios_base::in);
146 
147  string pid, comm, state, ppid, pgrp, session, tty_nr;
148  string tpgid, flags, minflt, cminflt, majflt, cmajflt;
149  string utime, stime, cutime, cstime, priority, nice;
150  string O, itrealvalue, starttime;
151 
152  unsigned long vsize;
153  long rss;
154 
155  stat_stream >> pid >> comm >> state >> ppid >> pgrp >> session >> tty_nr
156  >> tpgid >> flags >> minflt >> cminflt >> majflt >> cmajflt
157  >> utime >> stime >> cutime >> cstime >> priority >> nice
158  >> O >> itrealvalue >> starttime
159  >> vsize >> rss; // don't care about the rest
160 
161  stat_stream.close();
162 
163  // in case x86-64 is configured to use 2MB pages
164  long page_size = sysconf(_SC_PAGE_SIZE);
165 
166  // vm_usage = vsize / 1024.0;
167  // resident_set = rss * page_size;
168 
169  return rss * page_size;
170 
171 #else
172 
173  return 0;
174 
175 #endif
176 
177 }
178 
179 //----------------------------------------------------------------------------//
180 
182 
183 //----------------------------------------------------------------------------//
Msg::SevMessage
Definition: Log.h:67
Msg::SevWarning
Definition: Log.h:68
Msg
Contains logging-related functions.
Definition: Log.h:63
bytesToString
std::string bytesToString(int64_t bytes)
Converts a byte count into a human-readable string.
Definition: Log.cpp:101
Msg::Severity
Severity
Used by the Msg::print() call.
Definition: Log.h:66
Msg::setVerbosity
FIELD3D_API void setVerbosity(int level=1)
Set the verbosity level of console output: 0 = do not echo anything to the console; >=1 = echo all me...
Definition: Log.cpp:90
Log.h
Contains the Log class which can be used to redirect output to an arbitrary destination.
FIELD3D_NAMESPACE_SOURCE_CLOSE
#define FIELD3D_NAMESPACE_SOURCE_CLOSE
Definition: ns.h:60
currentRSS
size_t currentRSS()
Returns the current resident memory size.
Definition: Log.cpp:136
FIELD3D_NAMESPACE_OPEN
Definition: FieldMapping.cpp:74
Msg::print
FIELD3D_API void print(Severity severity, const std::string &message)
Sends the string to the assigned output, prefixing the message with the severity.
Definition: Log.cpp:70
Msg::g_verbosity
static int g_verbosity
Definition: Log.cpp:66