DataReelDataReelC++ Transition Guide

Examples › General Examples

general_logfile

C++17 or laterC++ standard library onlyPortable C++ (tested on Linux)

Legacy: examples_general/logfile/testprog.cpp · Modern: examples_modern/general/logfile/modern.cpp

Modern equivalent of the LogFile test. A log file is a std::ofstream opened for truncate (OverWrite()) or append; std::chrono::system_clock gives the time stamp (WriteSysTime(), GetSystemTime()). With C++20 std::format formats the time point directly; in C++17 the time is converted with std::localtime and printed with std::put_time. The file is read back with std::getline() (df_GetLine()).

Note: a production log should also do what LogFile does since 6.3: create the file owner-only and refuse to follow a symbolic link at the log file name. The standard library has no call for either, so use open() with O_NOFOLLOW and mode 0600 (POSIX) and wrap the descriptor, or set std::filesystem::permissions() right after creating the file.

Legacy calls in this program

LegacyModern C++StandardNotes
new / deleteautomatic objects, containers, std::unique_ptr / std::make_uniqueC++11 / C++14Ownership is in the type, and release() shows a hand-over.
DiskFileBstd::fstream (read, write, seekg/seekp, tellg)C++98
LogFilestd::ofstream + std::chrono::system_clock + std::formatC++11 / C++20Keep LogFile's 6.3 rules: owner-only file, no write through a symbolic link.
clock(), SysTime, GetSystemTime()std::chrono::steady_clock (elapsed), system_clock (calendar)C++11clock() is CPU time, not elapsed time.

From the transition tables.

Side by side

DataReel 6.4 (legacy)

examples_general/logfile/testprog.cpp 71 lines
#include "gxdlcode.h"

#if defined (__USE_ANSI_CPP__) // Use the ANSI Standard C++ library
#include <iostream>
using namespace std; // Use unqualified names for Standard C++ library
#else // Use the old iostream library by default
#include <iostream.h>
#endif // __USE_ANSI_CPP__

#include "logfile.h"

#ifdef __MSVC_DEBUG__
#include "leaktest.h"
#endif

static LogFile *SysLog;

int main()
{
#ifdef __MSVC_DEBUG__
  InitLeakTest();
#endif

  SysLog = new LogFile;
  char *fname = "logfile.log";
  
  char stime[255];
  cout << "Writing to logfile.log @ " << SysLog->GetSystemTime(stime) << "\n";
  if(SysLog->OverWrite(fname) != 0) {
    cout << "Could not write to log file" << "\n";
    return 0;
  }
  *(SysLog) << "Writing to logfile @";
  SysLog->WriteSysTime();
  *(SysLog) << "\n";

  SysLog->Flush();
  
  cout << "Appending 10 entries to logfile.log" << "\n";
  for(int i = 0; i < 10; i++) {
    *(SysLog) << text << "Appending to logfile [" << i << "]" << "\n";
  }
  
  cout << "Appending two strings to logfile" << "\n";
  *(SysLog) << "Appending string " << 1 << " and " << 2 << "\n";
  
  cout << "Appending three strings to logfile" << "\n";
  *(SysLog) << "Appending string " << 1.5 << ' ' << 2.5 << " and " << 3.5
	    << "\n";
  
  cout << "\n";
  cout << "Printing the logfile to the console" << "\n";
  cout << "\n";

  char sbuf[255];
  SysLog->df_Rewind();
  while(!SysLog->df_EOF()) {
    if(SysLog->df_GetLine(sbuf, sizeof(sbuf), '\n', 1) !=
       DiskFileB::df_NO_ERROR) {
      break;
    }
    cout << sbuf << "\n";
  }

  delete SysLog;
  return 0;
}
// ----------------------------------------------------------- // 
// ------------------------------- //
// --------- End of File --------- //
// ------------------------------- //

Modern C++ (C++17)

examples_modern/general/logfile/modern.cpp 75 lines
#include <chrono>
#include <ctime>
#include <filesystem>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <mutex>
#include <sstream>
#include <string>
#if __has_include(<format>)
#include <format>
#endif

std::string system_time()
{
  auto now = std::chrono::system_clock::now();
#if defined(__cpp_lib_format) && __cpp_lib_format >= 201907L
  return std::format("{:%Y-%m-%d %H:%M:%S}",
                     std::chrono::floor<std::chrono::seconds>(now));
#else
  std::time_t t = std::chrono::system_clock::to_time_t(now);
  std::tm tm_buf{};
#if defined(_WIN32)
  localtime_s(&tm_buf, &t);
#else
  localtime_r(&t, &tm_buf);   // Thread safe, unlike std::localtime
#endif
  std::ostringstream os;
  os << std::put_time(&tm_buf, "%Y-%m-%d %H:%M:%S");
  return os.str();
#endif
}

class Log {                    // A small thread safe log
public:
  explicit Log(const std::string &fname, bool append = false)
    : out_(fname, append ? std::ios::app : std::ios::trunc) {}
  explicit operator bool() const { return bool(out_); }
  template <class... Args>
  void write(const Args &...args) {
    std::lock_guard<std::mutex> lock(m_);
    (out_ << ... << args);     // C++17 fold expression
    out_ << "\n";
  }
  void flush() { out_.flush(); }
private:
  std::ofstream out_;
  std::mutex m_;
};

int main()
{
  const std::string fname = "logfile.log";
  std::cout << "Writing to " << fname << " @ " << system_time() << "\n";
  {
    Log log(fname);            // OverWrite()
    if(!log) { std::cout << "Could not write to log file\n"; return 1; }
    log.write("Writing to logfile @ ", system_time());
    std::cout << "Appending 10 entries to " << fname << "\n";
    for(int i = 0; i < 10; i++) log.write("Appending to logfile [", i, "]");
    log.write("Appending string ", 1, " and ", 2);
    log.write("Appending string ", 1.5, ' ', 2.5, " and ", 3.5);
  } // Closed and flushed here

  std::cout << "\nPrinting the logfile to the console\n\n";
  std::ifstream in(fname);
  for(std::string line; std::getline(in, line);) std::cout << line << "\n";
  in.close();
  std::filesystem::remove(fname);
  return 0;
}
// ----------------------------------------------------------- // 
// ------------------------------- //
// --------- End of File --------- //
// ------------------------------- //

Build and run

cmake -S examples_modern -B build-modern -DCMAKE_CXX_STANDARD=17
cmake --build build-modern --target general_logfile
./build-modern/general_logfile
# or without CMake:
g++ -std=c++17 -pthread -o general_logfile examples_modern/general/logfile/modern.cpp

Output of the modern program

From run_modern_tests.sh on RHEL 10, GCC 14, C++20.

Writing to logfile.log @ 2026-12-01 04:29:56
Appending 10 entries to logfile.log

Printing the logfile to the console

Writing to logfile @ 2026-12-01 04:29:56
Appending to logfile [0]
Appending to logfile [1]
Appending to logfile [2]
Appending to logfile [3]
Appending to logfile [4]
Appending to logfile [5]
Appending to logfile [6]
Appending to logfile [7]
Appending to logfile [8]
Appending to logfile [9]
Appending string 1 and 2
Appending string 1.5 2.5 and 3.5