Field3D
Log.h File Reference

Contains the Log class which can be used to redirect output to an arbitrary destination.
More...

#include <boost/lexical_cast.hpp>
#include "ns.h"

Go to the source code of this file.

Namespaces

 Msg
 Contains logging-related functions.
 

Enumerations

enum  Msg::Severity { Msg::SevMessage, Msg::SevWarning }
 Used by the Msg::print() call. More...
 

Functions

std::string bytesToString (int64_t bytes)
 Converts a byte count into a human-readable string. More...
 
size_t currentRSS ()
 Returns the current resident memory size. More...
 
void Msg::print (const std::string &message)
 Sends the string to the assigned output. More...
 
FIELD3D_API void Msg::print (Severity severity, const std::string &message)
 Sends the string to the assigned output, prefixing the message with the severity. More...
 
FIELD3D_API void Msg::setVerbosity (int level=1)
 Set the verbosity level of console output: 0 = do not echo anything to the console; >=1 = echo all messages and warnings to the console. More...
 

Detailed Description

Contains the Log class which can be used to redirect output to an arbitrary destination.

Definition in file Log.h.

Function Documentation

◆ bytesToString()

std::string bytesToString ( int64_t  bytes)

Converts a byte count into a human-readable string.

Definition at line 101 of file Log.cpp.

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 }

◆ currentRSS()

size_t currentRSS ( )

Returns the current resident memory size.

Warning
Currently only supported on Linux platform. Returns 0 for others.

Only implemented for Linux at the moment.

Definition at line 136 of file Log.cpp.

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 }