add support for log levels
[scrobby.git] / src / configuration.cpp
blob473a1801ce5cbd4cb6041214d49a6c69e8ad23a5
1 /***************************************************************************
2 * Copyright (C) 2008 by Andrzej Rybczak *
3 * electricityispower@gmail.com *
4 * *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
21 #include <iostream>
22 #include <fstream>
24 #include "configuration.h"
25 #include "misc.h"
27 using std::string;
29 namespace
31 std::string GetLineValue(const string &line, char a = '"', char b = '"')
33 int i = 0;
34 int begin = -1, end = -1;
35 for (string::const_iterator it = line.begin(); it != line.end(); i++, it++)
37 if (*it == a || *it == b)
39 if (begin < 0)
40 begin = i+1;
41 else
42 end = i;
45 if (begin >= 0 && end >= 0)
46 return line.substr(begin, end-begin);
47 else
48 return "";
51 LogLevel IntoLogLevel(const string &value)
53 LogLevel result = llInfo;
54 if (value == "none")
56 result = llNone;
58 else if (value == "info")
60 result = llInfo;
62 else if (value == "verbose")
64 result = llVerbose;
66 return result;
71 bool CheckFiles(ScrobbyConfig &conf)
73 std::ofstream f;
75 f.open(conf.file_log.c_str(), std::ios_base::app);
76 if (!f.is_open())
78 std::cerr << "Cannot create/open log file: " << conf.file_log << std::endl;
79 return false;
81 f.close();
83 f.open(conf.file_cache.c_str(), std::ios_base::app);
84 if (!f.is_open())
86 std::cerr << "Cannot create/open cache file: " << conf.file_cache << std::endl;
87 return false;
89 f.close();
91 std::ifstream g(conf.file_pid.c_str());
92 if (g.is_open())
94 string pid;
95 getline(g, pid);
96 std::cerr << "scrobby is already running with PID " << pid << "!\n";
97 return false;
99 f.open(conf.file_pid.c_str(), std::ios_base::app);
100 if (!f.is_open())
102 std::cerr << "Cannot create/open pid file: " << conf.file_pid << std::endl;
103 return false;
105 f.close();
107 return true;
110 void DefaultConfiguration(ScrobbyConfig &conf)
112 conf.mpd_host = "localhost";
113 conf.mpd_port = 6600;
114 conf.mpd_timeout = 15;
116 conf.file_log = "/var/log/scrobby.log";
117 conf.file_pid = "/var/run/scrobby.pid";
118 conf.file_cache = "/var/cache/scrobby/scrobby.cache";
120 conf.log_level = llInfo;
123 bool ReadConfiguration(ScrobbyConfig &conf, const string &file)
125 string line, v;
126 std::ifstream f(file.c_str());
128 if (!f.is_open())
129 return false;
131 while (!f.eof())
133 getline(f, line);
134 if (!line.empty() && line[0] != '#')
136 v = GetLineValue(line);
138 if (line.find("mpd_host") != string::npos)
140 if (!line.empty())
141 conf.mpd_host = v;
143 else if (line.find("mpd_password") != string::npos)
145 if (!v.empty())
146 conf.mpd_password = v;
148 else if (line.find("mpd_port") != string::npos)
150 if (!v.empty())
151 conf.mpd_port = StrToInt(v);
153 else if (line.find("mpd_timeout") != string::npos)
155 if (!v.empty())
156 conf.mpd_timeout = StrToInt(v);
158 else if (line.find("log_file") != string::npos)
160 if (!v.empty())
161 conf.file_log = v;
163 else if (line.find("pid_file") != string::npos)
165 if (!v.empty())
166 conf.file_pid = v;
168 else if (line.find("cache_file") != string::npos)
170 if (!v.empty())
171 conf.file_cache = v;
173 else if (line.find("lastfm_user") != string::npos)
175 if (!v.empty())
176 conf.lastfm_user = v;
178 else if (line.find("lastfm_password") != string::npos)
180 if (!v.empty())
181 conf.lastfm_password = v;
183 else if (line.find("lastfm_md5_password") != string::npos)
185 if (!v.empty())
186 conf.lastfm_md5_password = v;
188 else if (line.find("log_level") != string::npos)
190 if (!v.empty())
191 conf.log_level = IntoLogLevel(v);
195 f.close();
196 return true;