summaryrefslogtreecommitdiffstats
path: root/src/log.hpp
blob: 4d709b4725cb9ea599fd7913b50efae8e5ba8f51 (plain)
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
/*  This file is part of mastodonpp.
 *  Copyright © 2020 tastytea <tastytea@tastytea.de>
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Affero General Public License as published by
 *  the Free Software Foundation, version 3.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Affero General Public License for more details.
 *
 *  You should have received a copy of the GNU Affero General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#ifndef MASTODONPP_LOG_HPP
#define MASTODONPP_LOG_HPP

#include <iostream>
#include <string_view>

namespace mastodonpp
{

using std::cerr;
using std::string_view;

//! @private
constexpr auto shorten_filename(const string_view &filename)
{
    for (const string_view &dir : {"/src/", "/include/"})
    {
        const auto pos{filename.rfind(dir)};
        if (pos != string_view::npos)
        {
            return filename.substr(pos + dir.size());
        }
    }
    return filename;
}

#define commonlog cerr << '[' << shorten_filename(__FILE__) \
                       << ':' << __LINE__ << ']'
#ifndef NDEBUG
#define debuglog commonlog << " DEBUG: "
#else
    #define debuglog false && cerr
#endif
#define errorlog commonlog << " ERROR: "

} // namespace mastodonpp

#endif  // MASTODONPP_LOG_HPP
an>i = 0, isname = 0; *value = NULL; while(i<bufsize-1) { int c = next_char(f); if (!isname && (c=='#' || c==';')) { skip_line(f); continue; } if (!isname && isspace(c)) continue; if (c=='=' && !*value) { line[i] = 0; *value = &line[i+1]; } else if (c=='\n' && !isname) { i = 0; continue; } else if (c=='\n' || c==EOF) { line[i] = 0; break; } else { line[i]=c; } isname = 1; i++; } line[i+1] = 0; return i; } int parse_configfile(const char *filename, configfile_value_fn fn) { static int nesting; int len; char line[256]; const char *value; FILE *f; /* cancel deeply nested include-commands */ if (nesting > 8) return -1; if (!(f = fopen(filename, "r"))) return -1; nesting++; while((len = read_config_line(f, line, &value, sizeof(line))) > 0) fn(line, value); nesting--; fclose(f); return 0; }