Bredbandskollen CLI  1.2
Asynchronous network task engine
Public Member Functions | Static Public Member Functions | List of all members
TaskConfig Class Reference

Read configuration from file or string. More...

#include <taskconfig.h>

Public Member Functions

 TaskConfig ()
 Empty configuration.
 
 TaskConfig (std::istream &cfg_stream)
 Load configuration from file.
 
 TaskConfig (const std::string &cfg_text)
 Load configuration from string.
 
void add (const std::string &key, const std::string &val)
 Add a directive to the config.
 
void set (const std::string &key, const std::string &val)
 Replace value(s) of a directive with a new one.
 
void erase (const std::string &key)
 Remove value(s) of a directive.
 
void setDefault (const std::string &key, const std::string &val)
 Set value of a directive unless already set.
 
void addLine (const std::string &line)
 Incrementally add to the config.
 
std::multimap< std::string, std::string >::iterator begin ()
 Start iterator to loop over the config.
 
std::multimap< std::string, std::string >::iterator end ()
 End iterator to loop over the config.
 
std::multimap< std::string, std::string >::const_iterator begin () const
 Start const iterator to loop over the config.
 
std::multimap< std::string, std::string >::const_iterator end () const
 End const iterator to loop over the config.
 
void workerAttributes (const std::set< std::string > &attrs)
 Make a set of directives available to worker processes.
 
const std::multimap< std::string, std::string > & cfg () const
 Return the parsed configuration.
 
std::string value (const std::string &key) const
 Return value of last occurence of key, or empty string.
 
bool hasKey (const std::string &key) const
 Return true if key exists, otherwise false:
 
std::pair< std::multimap< std::string, std::string >::const_iterator, std::multimap< std::string, std::string >::const_iterator > range (const std::string &key) const
 Return a range of the key/value paris for the given key.
 
void openlog (std::ofstream &logger, bool append=false) const
 Log to the file specified by the logfile directive. More...
 
std::set< std::string > parseList (const std::string &category="whitelist") const
 Split config value into non-blank strings. More...
 
void parseArgs (int &argc, char **&argv)
 Parse command line arguments starting with "--":
 
std::map< std::string, std::string > parseKeyVal (const std::string &category="user") const
 
bool saveJsonToFile (const std::string &filename)
 Store contents as a JSON object. Return false on failure.
 

Static Public Member Functions

static TaskConfig load (const std::string &filename)
 Read config from file.
 
static TaskConfig loadJsonFromFile (const std::string &filename)
 Load key/value pairs from JSON object. More...
 

Detailed Description

Read configuration from file or string.

Empty lines are ignored.

A # character means the rest of the line is a comment.

All other lines must contain a configuration directive. If the line contains a space, the directive ends at the first space, and the rest of the line is the value of the directive.

Example:

logfile /var/log/my_service.log
name My Service  # Will be printed in greeting message

listen 80
listen 443 tls /etc/pki/tls/certs/mycert.pem /etc/pki/tls/private/mycert.pem 

Member Function Documentation

◆ loadJsonFromFile()

TaskConfig TaskConfig::loadJsonFromFile ( const std::string &  filename)
static

Load key/value pairs from JSON object.

Values that are not strings will be ignored. Return empty object on failure.

◆ openlog()

void TaskConfig::openlog ( std::ofstream &  logger,
bool  append = false 
) const

Log to the file specified by the logfile directive.

If the logfile key exists, and its value is not "-", try to use the value as a file name for the log.

◆ parseKeyVal()

std::map< std::string, std::string > TaskConfig::parseKeyVal ( const std::string &  category = "user") const

Return map of all key-value pairs of strings listed after the configuration directive given by second parameter.

◆ parseList()

std::set< std::string > TaskConfig::parseList ( const std::string &  category = "whitelist") const

Split config value into non-blank strings.

Return set of all non-whitespace strings listed after the configuration directive given by second parameter.


The documentation for this class was generated from the following files: