File: PluginFileLog.cpp

package info (click to toggle)
mediaconch 25.04-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 16,828 kB
  • sloc: ansic: 126,293; cpp: 39,636; javascript: 34,300; xml: 2,950; sh: 2,121; makefile: 200; python: 183
file content (127 lines) | stat: -rw-r--r-- 3,855 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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
124
125
126
127
/*  Copyright (c) MediaArea.net SARL. All Rights Reserved.
 *
 *  Use of this source code is governed by a BSD-style license that can
 *  be found in the License.html file in the root of the source tree.
 */

//---------------------------------------------------------------------------
#include "PluginFileLog.h"

//---------------------------------------------------------------------------
namespace MediaConch {

//***************************************************************************
// Class PluginFileLog
//***************************************************************************

//---------------------------------------------------------------------------
PluginFileLog::PluginFileLog()
{
    type = MediaConchLib::PLUGIN_LOG;
}

//---------------------------------------------------------------------------
PluginFileLog::~PluginFileLog()
{
    if (this->is_open())
        this->close();
    this->filename.clear();
}

//---------------------------------------------------------------------------
PluginFileLog::PluginFileLog(const PluginFileLog& p) : PluginLog(p)
{
    filename = p.filename;
}

//---------------------------------------------------------------------------
int PluginFileLog::load_plugin(const std::map<std::string, Container::Value>& obj, std::string& error)
{
    if (obj.find("file") == obj.end() || obj.at("file").type != Container::Value::CONTAINER_TYPE_STRING)
    {
        error += "Field 'file' is not present\n";
        return -1;
    }
    this->set_file(obj.at("file").s);

    if (obj.find("level") != obj.end() && obj.at("level").type == Container::Value::CONTAINER_TYPE_STRING)
    {
        std::string level(obj.at("level").s);
        if (level == "nothing")
            this->set_level(PluginLog::LOG_LEVEL_NOTHING);
        else if (level == "all" || level == "debug")
            this->set_level(PluginLog::LOG_LEVEL_DEBUG);
        else if (level == "warning")
            this->set_level(PluginLog::LOG_LEVEL_WARNING);
        else if (level == "error")
            this->set_level(PluginLog::LOG_LEVEL_ERROR);
    }

    return 0;
}

//---------------------------------------------------------------------------
int PluginFileLog::run(std::string& error)
{
    error = "Nothing to do for Plugin Log";
    return -1;
}

//---------------------------------------------------------------------------
void PluginFileLog::set_file(const std::string& file)
{
    file_mutex.Enter();
    this->filename = file;
    file_mutex.Leave();

    this->close();
    this->open();
}

//---------------------------------------------------------------------------
void PluginFileLog::add_log(const std::string& time, int l, const std::string& log)
{
    if (l >= this->level && this->is_open())
    {
        file_mutex.Enter();
        this->file_handle << time << ":" << log << std::endl;
        file_mutex.Leave();
    }
}

//---------------------------------------------------------------------------
bool PluginFileLog::is_open()
{
    if (!this->filename.length())
        return false;

    file_mutex.Enter();
    bool is = this->file_handle.is_open();
    file_mutex.Leave();
    return is;
}

//---------------------------------------------------------------------------
bool PluginFileLog::open()
{
    if (!this->filename.length())
        return false;

    file_mutex.Enter();
    this->file_handle.open(this->filename.c_str(), std::ofstream::out | std::ofstream::app);
    file_mutex.Leave();
    return this->is_open();
}

//---------------------------------------------------------------------------
void PluginFileLog::close()
{
    if (this->is_open())
    {
        file_mutex.Enter();
        this->file_handle.close();
        file_mutex.Leave();
    }
}

}