Log.cpp File Reference

Contains implementations of the logging-related functions. More...

`#include <unistd.h>`
`#include <ios>`
`#include <fstream>`
`#include <iostream>`
`#include "Log.h"`

Go to the source code of this file.

## Namespaces

namespace | Msg
| Contains logging-related functions.

## Functions

std::string | bytesToString (int64_t bytes)
| Converts a byte count into a human-readable string.
size_t | currentRSS ()
| Returns the current resident memory size.
FIELD3D_API void | Msg::print (Severity severity, const std::string &message)
| Sends the string to the assigned output, prefixing the message with the severity.
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.

## Variables

static int | Msg::g_verbosity = 1

## Detailed Description

Contains implementations of the logging-related functions.

Definition in file Log.cpp.

## 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.

{
using std::stringstream;
stringstream ss;
ss.precision(3);
ss.setf(std::ios::fixed, std:: ios::floatfield);
// Make it work for negative numbers
if (bytes < 0) {
ss << "-";
bytes = -bytes;
}
if (bytes < 1024) {
// Bytes
ss << bytes << " B";
return ss.str();
} else if (bytes < (1024 * 1024)) {
// Kilobytes
ss << bytes / static_cast<float>(1024) << " KB";
return ss.str();
} else if (bytes < (1024 * 1024 * 1024)) {
// Megabytes
ss << bytes / static_cast<float>(1024 * 1024) << " MB";
return ss.str();
} else {
// Gigabytes
ss << bytes / static_cast<float>(1024 * 1024 * 1024) << " GB";
return ss.str();
}
}

## 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.

{
#ifdef __linux__
using std::ios_base;
using std::ifstream;
using std::string;
ifstream stat_stream("/proc/self/stat", ios_base::in);
string pid, comm, state, ppid, pgrp, session, tty_nr;
string tpgid, flags, minflt, cminflt, majflt, cmajflt;
string utime, stime, cutime, cstime, priority, nice;
string O, itrealvalue, starttime;
unsigned long vsize;
long rss;
stat_stream >> pid >> comm >> state >> ppid >> pgrp >> session >> tty_nr
>> tpgid >> flags >> minflt >> cminflt >> majflt >> cmajflt
>> utime >> stime >> cutime >> cstime >> priority >> nice
>> O >> itrealvalue >> starttime
>> vsize >> rss; // don't care about the rest
stat_stream.close();
// in case x86-64 is configured to use 2MB pages
long page_size = sysconf(_SC_PAGE_SIZE);
// vm_usage = vsize / 1024.0;
// resident_set = rss * page_size;
return rss * page_size;
#else
return 0;
#endif
}