<commit_before>/*
 *
 * Author: <NAME>
 * Last edited: 2016-03-04
 *
 * This C++ file contains the function implemenations for the
 * exception logger Exceptional.
 *
 */

#include <iostream>
#include <string>

#include "../include/exceptional.hpp"

namespace
{

// This local function returns the current time as a string, formatted in
// the ISO 8601 standard.
std::string GetTime();

// This local function returns the current time as a string, formatted in
// the ISO 8601 standard.
std::string GetTime()
{
  time_t raw_time;
  time( &raw_time );

  char buffer[ sizeof("1970-01-01T00:00:00") ];
  strftime(buffer, sizeof(buffer), "%Y-%m-%dT%H:%M:%S", localtime(&raw_time));
  return buffer;
}

}  // End of unnamed namespace (for local functions)

namespace exceptional
{
  // Constructor
  Logger::Logger()
  {
    // Opens the file in append mode
    log_stream_.open( log_filename_, std::ios::app );

    log_stream_
      << "________________________________________"
      << std::endl
      << std::endl
      << "LOG CREATED AT "
      << GetTime()
      << std::endl
      << std::endl;
  }

  // Destructor
  Logger::~Logger()
  {
    log_stream_
      << "Warnings logged: "
      << warning_count_
      << std::endl
      << "Errors logged:   "
      << error_count_
      << std::endl
      << std::endl;

    log_stream_.close();
  }

  // This public method logs a thrown std::exception as a warning.
  void Logger::LogWarning( const std::exception& except )
  {
    ++warning_count_;
    LogSeverityLevel( SeverityLevel::kWarning );
    LogTime();
    LogExceptionType( except );
    LogExceptionMessage( except );
    log_stream_ << std::endl;
  }

  // This public method logs a thrown std::exception as an error.
  void Logger::LogError( const std::exception& except )
  {
    ++error_count_;
    LogSeverityLevel( SeverityLevel::kError );
    LogTime();
    LogExceptionType( except );
    LogExceptionMessage( except );
    log_stream_ << std::endl;
  }

  // This private method logs the severity level of the exception.
  void Logger::LogSeverityLevel( SeverityLevel sl )
  {
    std::string output;
    switch( sl )
    {
      case SeverityLevel::kWarning:
        output = "Warning:";
        break;
      case SeverityLevel::kError:
        output = "Error:";
        break;
    }

    log_stream_
      << output
      << std::endl;
  }

  // This private method logs the time of a thrown exception.
  void Logger::LogTime()
  {
    log_stream_
      << "Logged at: "
      << GetTime()
      << std::endl;
  }

  // This private method logs the message of an exception from std.
  void Logger::LogExceptionMessage( const std::exception& except )
  {
    log_stream_
      << "Exception message: "
      << except.what();

    std::string s = except.what();
    if( s[s.length()-1] != '\n' )
    {
      log_stream_
        << std::endl;
    }
  }

}  // End of namespace exceptional<commit_msg>Removing namespace indentation<commit_after>/*
 *
 * Author: <NAME>
 * Last edited: 2016-03-04
 *
 * This C++ file contains the function implemenations for the
 * exception logger Exceptional.
 *
 */

#include <iostream>
#include <string>

#include "../include/exceptional.hpp"

namespace
{

// This local function returns the current time as a string, formatted in
// the ISO 8601 standard.
std::string GetTime();

// This local function returns the current time as a string, formatted in
// the ISO 8601 standard.
std::string GetTime()
{
  time_t raw_time;
  time( &raw_time );

  char buffer[ sizeof("1970-01-01T00:00:00") ];
  strftime(buffer, sizeof(buffer), "%Y-%m-%dT%H:%M:%S", localtime(&raw_time));
  return buffer;
}

}  // End of unnamed namespace (for local functions)

namespace exceptional
{

// Constructor
Logger::Logger()
{
  // Opens the file in append mode
  log_stream_.open( log_filename_, std::ios::app );

  log_stream_
    << "________________________________________"
    << std::endl
    << std::endl
    << "LOG CREATED AT "
    << GetTime()
    << std::endl
    << std::endl;
}

// Destructor
Logger::~Logger()
{
  log_stream_
    << "Warnings logged: "
    << warning_count_
    << std::endl
    << "Errors logged:   "
    << error_count_
    << std::endl
    << std::endl;

  log_stream_.close();
}

// This public method logs a thrown std::exception as a warning.
void Logger::LogWarning( const std::exception& except )
{
  ++warning_count_;
  LogSeverityLevel( SeverityLevel::kWarning );
  LogTime();
  LogExceptionType( except );
  LogExceptionMessage( except );
  log_stream_ << std::endl;
}

// This public method logs a thrown std::exception as an error.
void Logger::LogError( const std::exception& except )
{
  ++error_count_;
  LogSeverityLevel( SeverityLevel::kError );
  LogTime();
  LogExceptionType( except );
  LogExceptionMessage( except );
  log_stream_ << std::endl;
}

// This private method logs the severity level of the exception.
void Logger::LogSeverityLevel( SeverityLevel sl )
{
  std::string output;
  switch( sl )
  {
    case SeverityLevel::kWarning:
      output = "Warning:";
      break;
    case SeverityLevel::kError:
      output = "Error:";
      break;
  }

  log_stream_
    << output
    << std::endl;
}

// This private method logs the time of a thrown exception.
void Logger::LogTime()
{
  log_stream_
    << "Logged at: "
    << GetTime()
    << std::endl;
}

// This private method logs the message of an exception from std.
void Logger::LogExceptionMessage( const std::exception& except )
{
  log_stream_
    << "Exception message: "
    << except.what();

  std::string s = except.what();
  if( s[s.length()-1] != '\n' )
  {
    log_stream_
      << std::endl;
  }
}

}  // End of namespace exceptional
{'fraction_non_alphanumeric': 0.13464235624123422, 'fraction_numerical': 0.010869565217391304, 'mean_word_length': 3.0403682719546743, 'pattern_counts': {'":': 0, '<': 105, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '191643', 'n_tokens_mistral': 1842, 'n_tokens_neox': 1777, 'n_words': 493}