Does log4cplus support xml configuration file and conditional logging? - c++

I want to use MDC to set a context in my program, and then be able to set a filter in the config file to only show messages from a logger in a certain context.
MDC mdc;
mdc.put("fieldName", "foo");
LOG4CPLUS_DEBUG(log, "ABC");
mdc.put("fieldName", "bar");
LOG4CPLUS_DEBUG(log, "XYZ");
I only want to print from log when "fieldName" is "bar", so only "XYZ" should be printed to my log.
In log4j this can be done using the XML format config file with filters. Can log4cplus use an XML config file? Can filters like this be set up in the XML log?
Also, is there a way to make log4cplus verbose, so I can see if it is finding my config file, and if it is able to read it, etc.
I am having trouble finding even one example of a log4cplus XML config file or how to read one.

Log4cplus does not support any kind of XML configuration file. This is because it would require an XML parser and that is a pretty heavy dependency.
As for the filtering using MDC, there is currently no such filter. You would have to implement your own. See the Filter class.
You can set up log4cplus debugging from property file using log4cplus.configDebug=1. Or you can set the LOG4CPLUS_LOGLOG_DEBUGENABLED environment variable to 1. The former takes precedence over the latter.

Related

QT C++ Project (Creating Login with XML)

I have question about my internship project. They want me to create a basic Login page(ID, Password). I create a XML file for Username and Password. The program should check the XML file for username and password*. If they are correct it will direct to a second window. I'm stuck on processing XML file for username and password. How can read those information in XML file.
As #JarMan said, I would recommend the QXmlStreamReader. You can fill it with a file (QIODevice), QString, QByteArray, etc...
Parsing a value could e.g. look like that
xml.attributes().value( attribute ).toString();
if attribute is a QString and xml is the QXmlStreamReader.
See the doc https://doc.qt.io/qt-5/qxmlstreamreader.html
There are several ways to do it. Marris mentioned one, but another one is to have this kind of code generated automatically. The way this works is that you first write an XML Schema that describes what your XML data looks like. An introduction to the XML Schema language can be found e. g. here.
Then you use an XML Schema compiler to translate the XML Schema to C++ classes. The schema compiler will also generate code to parse an XML file into objects, meaning you don't have to write any code to deal with XML by hand. It's a purely declarative approach: declare what the data looks like, and let the computer figure out the details.

Set OSM config options using API and not the global config file

When reading OSM files using GDAL, the fields that are read are defined in osmconf.ini, and if I want that certain tags don't appear within the other_tags then I need to add them to the attributes value in the corresponding sections.
This works fine, but is not really portable, so my questions is, is there a ways to define the settings saved in osmconf.ini in a portable way per project?
This is possible using the CPLSetConfigOption function, and store the config file in current working directory:
CPLSetConfigOption("OSM_CONFIG_FILE", "osmconf.ini");

How do I add comments to my config file using QSettings?

I'm writing a c++ code using qt and need an editable config file for my user to change some settings. In order to provide him some additional information I would like to add comments to my config file, however I cant find a way to mark them as comments.
I am using QSettings, my file is a .flt file. The usual '#' unfortunately does not seem to work with QSettings.
when using setting files in Qt and the QSettings class, you don't use the "usual"
#
for defining a comment, but the
;
instead...
so:
[abc]
key=val
;this is a comment in the QSettings
flag=true
QSetting's INI file format uses MS Windows file format, which is
a) hierarchical and uses brackets [] for section names
b) uses ; to designate comment lines.
Note, thr default engine of QSetting would wipe any comments, because the whole mechanism is just serialization of name-value pairs from file and to file. To avoid that, a custom reader-writer class should be devised which would read and preserve comments somehow. QSettings supports custom formats by offering interface for read and write functions.

Xerces-C validate xml with hardcoded xsd

I'm writing a library which takes xml files and parses them. To prevent users from feeding inalid xmls into my application i'm using xerces to validate the xml files via an xsd.
However, i only manages to validate against xsd-files. Theoretically an user could just open this file and mess around with it. That's why i would like my xsd to be hardcoded in my library.
Unfortunately i haven't found a way to do this with XercesC++, yet.
That's how it is working right now...
bool XmlParser::validateXml(std::string a_XsdFilename)
{
xercesc::XercesDOMParser domParser;
if (domParser.loadGrammar(a_XsdFilename.c_str(), xercesc::Grammar::SchemaGrammarType) == NULL)
{
throw Exceptions::Parser::XmlSchemaNotReadableException();
}
XercesParserErrorHandler parserErrorHandler;
domParser.setErrorHandler(&parserErrorHandler);
domParser.setValidationScheme(xercesc::XercesDOMParser::Val_Always);
domParser.setDoNamespaces(true);
domParser.setDoSchema(true);
domParser.setValidationSchemaFullChecking(true);
domParser.parse(m_Filename.c_str());
return (domParser.getErrorCount() == 0);
}
std::string m_Filename is a member variable holding the path of the xml i validate.
std::string a_XsdFilename is the path to the xsd i validate against.
XercesParserErrorHandler inherits from xercesc::ErrorHandler and does error handling.
How can i replace std::string a_XsdFilename with something like std::string a_XsdText?
Where std::string a_XsdText contains the schema definition itself instead of a path to a file containing the schema definition.
I'll describe three ways of how to hardcode your XSD in your program:
by loading the XSD from a file path (this is what your example program does right now)
by loading the XSD from a string (this is what you ask for)
by loading the XSD from a precompiled binary
Loading the XSD from a file path
Boris Kolpackov suggests in a blog post that applications should provide the XSD schema files by themselves rather than looking up the schema files through the xsi:schemaLocation or xsi:noNamespaceSchemaLocation attributes found in the XML file.
In the blog post there is a link to load-grammar-dom , an example program (put in the public domain) that makes use of the xercesc::DOMLSParser::loadGrammar function:
user#linux:~$ load-grammar-dom
usage: load-grammar-dom [test.xsd ... ] [test.xml ...]
user#linux:~$
Loading the XSD from a string
If you would like to pass the XSD file contents as a string, you would need to use another overload of
xercesc::DOMLSParser::loadGrammar
where you pass
const DOMLSInput *source
instead of
const char *const systemId
The DOMLSInput could be created with the help of xercesc::MemBufInputSource and xercesc::Wrapper4InputSource like this
xercesc::Wrapper4InputSource source(
new xercesc::MemBufInputSource(
(const XMLByte *) (a_XsdText.c_str()),
a_XsdText.size(),
"A name");
(Adapted somewhat from
https://stackoverflow.com/a/15829424/757777 but untested)
Loading the XSD from a precompiled binary
Included in the software CodeSynthesis XSD the embedded example (that is put in the public domain) demonstrates how to use
xercesc::BinInputStream and
xercesc::XMLGrammarPool::deserializeGrammars
to load a precompiled XSD schema.
See also README.
The example contains the program xsdbin that compiles XSD schema files into a binary file.
user#linux:~$ xsdbin --help
Usage: xsdbin [options] <files>
Options:
--help Print usage information and exit.
--verbose Print progress information.
--output-dir <dir> Write generated files to <dir>.
--hxx-suffix <sfx> Header file suffix instead of '-schema.hxx'.
--cxx-suffix <sfx> Source file suffix instead of '-schema.cxx'.
--array-name <name> Binary data array name.
--disable-multi-import Disable multiple import support.
user#linux:~$
In the makefile the XSD schema file is precompiled by xsdbin and the result ends up inside the example executable.

how to use wicket XsltTransformerBehavior when I have multiple .xsl files

I'm working part of a project that needs to display a .xml file with .xsl files.
Here's part of the code:
FILE_PATH = "myxsl.xsl"
...
Label content = new Label("content",xmlContent);
content.setEscapeModelStrings(false);
content.add(new XsltTransformerBehavior(FILE_PATH));
add(content);
...
Currently the page works if I use only one .xsl file. However, because the .xsl files I need to deal with can be really long, they are separated into several components.
for example, I will have
mymain.xsl, head.xsl, tables.xsl
the mymain.xsl has inclusion of the other like this
<xsl:include href="head.xsl"/>
<xsl:include href="tables.xsl"/>
I tried to set FILE_PATH to mymain.xsl, but it didn't work. The program can find mymain.xsl but cannot compile the stylesheet because it cannot find head.xsl and tables.xsl
I've been searching for a long time but still have no clue how to do this. Really appreciate any help. Thanks in advance.
I looked at the documentation of XsltTransformerBehavior, and it looks like it doesn't expose the functionality to set a custom URI resolver (which is basically what you want to do).
However, in the case that no-one else has a better solution, I can propose a workaround: Write an XSLT that merges your XSLT files into one file. See here for an XSLT that does this. If you're using Maven as build system, you can do this automatically in the generate-resources phase using the transform goal of the Maven XML Plugin. You can then associate the XsltTransformerBehavior with the single-file XSLT which will be available at run-time.