log.cpp 3.05 KB
Newer Older
Robert Kimball's avatar
Robert Kimball committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/*
 Copyright 2016 Nervana Systems Inc.
 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
*/

#include <chrono>
17 18
#include <condition_variable>
#include <ctime>
Robert Kimball's avatar
Robert Kimball committed
19 20 21
#include <iomanip>
#include <iostream>
#include <mutex>
22
#include <thread>
Robert Kimball's avatar
Robert Kimball committed
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123

#include "log.hpp"

using namespace std;

namespace nervana
{
    class thread_starter;
}

string                    nervana::logger::log_path;
deque<string>             nervana::logger::queue;
static mutex              queue_mutex;
static condition_variable queue_condition;
static unique_ptr<thread> queue_thread;
static bool               active = false;

class nervana::thread_starter
{
public:
    thread_starter() { nervana::logger::start(); }
    virtual ~thread_starter() { nervana::logger::stop(); }
};

static nervana::thread_starter _starter;

void nervana::logger::set_log_path(const string& path)
{
    log_path = path;
}

void nervana::logger::start()
{
    active       = true;
    queue_thread = unique_ptr<thread>(new thread(&thread_entry, nullptr));
}

void nervana::logger::stop()
{
    {
        unique_lock<std::mutex> lk(queue_mutex);
        active = false;
        queue_condition.notify_one();
    }
    queue_thread->join();
}

void nervana::logger::process_event(const string& s)
{
    cout << s << "\n";
}

void nervana::logger::thread_entry(void* param)
{
    unique_lock<std::mutex> lk(queue_mutex);
    while (active)
    {
        queue_condition.wait(lk);
        while (!queue.empty())
        {
            process_event(queue.front());
            queue.pop_front();
        }
    }
}

void nervana::logger::log_item(const string& s)
{
    unique_lock<std::mutex> lk(queue_mutex);
    queue.push_back(s);
    queue_condition.notify_one();
}

nervana::log_helper::log_helper(LOG_TYPE type, const char* file, int line, const char* func)
{
    switch (type)
    {
    case LOG_TYPE::_LOG_TYPE_ERROR: _stream << "[ERR ] "; break;
    case LOG_TYPE::_LOG_TYPE_WARNING: _stream << "[WARN] "; break;
    case LOG_TYPE::_LOG_TYPE_INFO: _stream << "[INFO] "; break;
    }

    std::time_t tt = chrono::system_clock::to_time_t(chrono::system_clock::now());
    auto        tm = std::gmtime(&tt);
    char        buffer[256];
    //    strftime(buffer,sizeof(buffer), "%d/%b/%Y:%H:%M:%S %z", tm);
    //    strftime(buffer,sizeof(buffer), "%Y-%m-%d %H:%M:%S UTC", tm);
    strftime(buffer, sizeof(buffer), "%Y-%m-%dT%H:%M:%Sz", tm);
    _stream << buffer << " ";

    _stream << file;
    _stream << " " << line;
    //    _stream << " " << func;
    _stream << "\t";
}

nervana::log_helper::~log_helper()
{
    cout << _stream.str() << endl;
    // logger::log_item(_stream.str());
}