<commit_before>

/**
   @file main.cpp
   @brief Sensord initiation point

   <p>
   Copyright (C) 2009-2010 Nokia Corporation

   @author <NAME> <<EMAIL>>
   @author <NAME> <<EMAIL>>
   @author <NAME> <<EMAIL>>
   @author <NAME> <<EMAIL>>
   @author <NAME> <<EMAIL>>
   @author <NAME> <<EMAIL>

   This file is part of Sensord.

   Sensord is free software; you can redistribute it and/or modify
   it under the terms of the GNU Lesser General Public License
   version 2.1 as published by the Free Software Foundation.

   Sensord 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
   Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public
   License along with Sensord.  If not, see <http://www.gnu.org/licenses/>.
   </p>
 */

#include <QtCore/QCoreApplication>
#include <QtCore/QDebug>

#include <signal.h>
#include <iostream>
#include <errno.h>

#include "config.h"
#include "sensormanager.h"
#include "sensormanager_a.h"
#include "logging.h"
#include "calibrationhandler.h"
#include "parser.h"

static QtMsgType logLevel;
static QtMessageHandler previousMessageHandler;

static void messageOutput(QtMsgType type, QMessageLogContext &context, const QString &str)
{
    if (type < logLevel)
        return;

    previousMessageHandler(type, context, str);
}

void printUsage();

void signalUSR1(int param)
{
    Q_UNUSED(param);

    logLevel = QtMsgType(logLevel + 1);
    if (logLevel > QtSystemMsg)
        logLevel = QtDebugMsg;
    std::cerr << "New debugging level: " << logLevel << "\n";
}

void signalUSR2(int param)
{
    Q_UNUSED(param);

    QStringList output;

    output.append("Flushing sensord state\n");
    output.append(QString("  Logging level: %1\n").arg(logLevel));
    SensorManager::instance().printStatus(output);

    foreach (const QString& line, output) {
        sensordLogW() << line.toLocal8Bit().data();
    }
}

void signalINT(int param)
{
    Q_UNUSED(param);
    QCoreApplication::exit(0);
}

int main(int argc, char *argv[])
{
    QCoreApplication app(argc, argv);
    SensorManager& sm = SensorManager::instance();
    Parser parser(app.arguments());

    if (parser.printHelp())
    {
        printUsage();
        app.exit(EXIT_SUCCESS);
        return 0;

    }

    logLevel = parser.getLogLevel();

    const char* CONFIG_FILE_PATH = "/etc/sensorfw/sensord.conf";
    const char* CONFIG_DIR_PATH = "/etc/sensorfw/sensord.conf.d/";

    QString defConfigFile = CONFIG_FILE_PATH;
    if(parser.configFileInput())
    {
        defConfigFile = parser.configFilePath();
    }

    QString defConfigDir = CONFIG_DIR_PATH;
    if(parser.configDirInput())
    {
        defConfigDir = parser.configDirPath();
    }

    if (!Config::loadConfig(defConfigFile, defConfigDir))
    {
        sensordLogC() << "Config file error! Load using default paths.";
        if (!Config::loadConfig(CONFIG_FILE_PATH, CONFIG_DIR_PATH))
        {
            sensordLogC() << "Which also failed. Bailing out";
            return 1;
        }
    }

    signal(SIGUSR1, signalUSR1);
    signal(SIGUSR2, signalUSR2);
    signal(SIGINT, signalINT);

#ifdef PROVIDE_CONTEXT_INFO
    if (parser.contextInfo())
    {
        sensordLogD() << "Loading ContextSensor " << sm.loadPlugin("contextsensor");
        sensordLogD() << "Loading ALSSensor " << sm.loadPlugin("alssensor");
    }
#endif

    
    if (parser.createDaemon())
    {
        int pid = fork();

        if(pid < 0)
        {
            sensordLogC() << "Failed to create a daemon: " << strerror(errno);
            exit(EXIT_FAILURE);
        } else if (pid > 0) {
            sensordLogW() << "Created a daemon";
            exit(EXIT_SUCCESS);
        }
    }

    if (parser.magnetometerCalibration())
    {
        CalibrationHandler* calibrationHandler_ = new CalibrationHandler(NULL);
        calibrationHandler_->initiateSession();
        QObject::connect(&sm, SIGNAL(resumeCalibration()), calibrationHandler_, SLOT(resumeCalibration()));
        QObject::connect(&sm, SIGNAL(stopCalibration()), calibrationHandler_, SLOT(stopCalibration()));
    }

    if (!sm.registerService())
    {
        sensordLogW() << "Failed to register service on D-Bus. Aborting.";
        exit(EXIT_FAILURE);
    }

    int ret = app.exec();
    sensordLogD() << "Exiting...";
    Config::close();
    return ret;
}

void printUsage()
{
    qDebug() << "Usage: sensord [OPTIONS]";
    qDebug() << " -d, --daemon                     Detach from terminal and run as daemon.\n";
    qDebug() << " -l=N, --log-level=<level>        Use given logging level. Messages are logged for";
    qDebug() << "                                  the given and higher priority levels. Level";
    qDebug() << "                                  can also be notched up by sending SIGUSR1 to";
    qDebug() << "                                  the process. Valid values for N are: 'test',";
    qDebug() << "                                  'debug', 'warning', 'critical'.\n";
    qDebug() << " -c=P, --config-file=<path>       Load configuration from given path. By default";
    qDebug() << "                                  /etc/sensorfw/sensord.conf is used.\n";
    qDebug() << " --no-context-info                Do not provide context information for context";
    qDebug() << "                                  framework.\n";
    qDebug() << " --no-magnetometer-bg-calibration Do not start calibration of magnetometer in";
    qDebug() << "                                  the background.\n";
    qDebug() << " -h, --help                       Show usage info and exit.";
}
<commit_msg>[sensorfw] Actually install the message handler.<commit_after>

/**
   @file main.cpp
   @brief Sensord initiation point

   <p>
   Copyright (C) 2009-2010 Nokia Corporation

   @author <NAME> <<EMAIL>>
   @author <NAME> <<EMAIL>>
   @author <NAME> <<EMAIL>>
   @author <NAME> <<EMAIL>>
   @author <NAME> <<EMAIL>>
   @author <NAME> <<EMAIL>

   This file is part of Sensord.

   Sensord is free software; you can redistribute it and/or modify
   it under the terms of the GNU Lesser General Public License
   version 2.1 as published by the Free Software Foundation.

   Sensord 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
   Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public
   License along with Sensord.  If not, see <http://www.gnu.org/licenses/>.
   </p>
 */

#include <QtCore/QCoreApplication>
#include <QtCore/QDebug>

#include <signal.h>
#include <iostream>
#include <errno.h>

#include "config.h"
#include "sensormanager.h"
#include "sensormanager_a.h"
#include "logging.h"
#include "calibrationhandler.h"
#include "parser.h"

static QtMsgType logLevel;
static QtMessageHandler previousMessageHandler;

static void messageOutput(QtMsgType type, const QMessageLogContext &context, const QString &str)
{
    if (type < logLevel)
        return;

    previousMessageHandler(type, context, str);
}

void printUsage();

void signalUSR1(int param)
{
    Q_UNUSED(param);

    logLevel = QtMsgType(logLevel + 1);
    if (logLevel > QtSystemMsg)
        logLevel = QtDebugMsg;
    std::cerr << "New debugging level: " << logLevel << "\n";
}

void signalUSR2(int param)
{
    Q_UNUSED(param);

    QStringList output;

    output.append("Flushing sensord state\n");
    output.append(QString("  Logging level: %1\n").arg(logLevel));
    SensorManager::instance().printStatus(output);

    foreach (const QString& line, output) {
        sensordLogW() << line.toLocal8Bit().data();
    }
}

void signalINT(int param)
{
    Q_UNUSED(param);
    QCoreApplication::exit(0);
}

int main(int argc, char *argv[])
{
    previousMessageHandler = qInstallMessageHandler(messageOutput);

    QCoreApplication app(argc, argv);
    SensorManager& sm = SensorManager::instance();
    Parser parser(app.arguments());

    if (parser.printHelp())
    {
        printUsage();
        app.exit(EXIT_SUCCESS);
        return 0;

    }

    logLevel = parser.getLogLevel();

    const char* CONFIG_FILE_PATH = "/etc/sensorfw/sensord.conf";
    const char* CONFIG_DIR_PATH = "/etc/sensorfw/sensord.conf.d/";

    QString defConfigFile = CONFIG_FILE_PATH;
    if(parser.configFileInput())
    {
        defConfigFile = parser.configFilePath();
    }

    QString defConfigDir = CONFIG_DIR_PATH;
    if(parser.configDirInput())
    {
        defConfigDir = parser.configDirPath();
    }

    if (!Config::loadConfig(defConfigFile, defConfigDir))
    {
        sensordLogC() << "Config file error! Load using default paths.";
        if (!Config::loadConfig(CONFIG_FILE_PATH, CONFIG_DIR_PATH))
        {
            sensordLogC() << "Which also failed. Bailing out";
            return 1;
        }
    }

    signal(SIGUSR1, signalUSR1);
    signal(SIGUSR2, signalUSR2);
    signal(SIGINT, signalINT);

#ifdef PROVIDE_CONTEXT_INFO
    if (parser.contextInfo())
    {
        sensordLogD() << "Loading ContextSensor " << sm.loadPlugin("contextsensor");
        sensordLogD() << "Loading ALSSensor " << sm.loadPlugin("alssensor");
    }
#endif

    
    if (parser.createDaemon())
    {
        int pid = fork();

        if(pid < 0)
        {
            sensordLogC() << "Failed to create a daemon: " << strerror(errno);
            exit(EXIT_FAILURE);
        } else if (pid > 0) {
            sensordLogW() << "Created a daemon";
            exit(EXIT_SUCCESS);
        }
    }

    if (parser.magnetometerCalibration())
    {
        CalibrationHandler* calibrationHandler_ = new CalibrationHandler(NULL);
        calibrationHandler_->initiateSession();
        QObject::connect(&sm, SIGNAL(resumeCalibration()), calibrationHandler_, SLOT(resumeCalibration()));
        QObject::connect(&sm, SIGNAL(stopCalibration()), calibrationHandler_, SLOT(stopCalibration()));
    }

    if (!sm.registerService())
    {
        sensordLogW() << "Failed to register service on D-Bus. Aborting.";
        exit(EXIT_FAILURE);
    }

    int ret = app.exec();
    sensordLogD() << "Exiting...";
    Config::close();
    return ret;
}

void printUsage()
{
    qDebug() << "Usage: sensord [OPTIONS]";
    qDebug() << " -d, --daemon                     Detach from terminal and run as daemon.\n";
    qDebug() << " -l=N, --log-level=<level>        Use given logging level. Messages are logged for";
    qDebug() << "                                  the given and higher priority levels. Level";
    qDebug() << "                                  can also be notched up by sending SIGUSR1 to";
    qDebug() << "                                  the process. Valid values for N are: 'test',";
    qDebug() << "                                  'debug', 'warning', 'critical'.\n";
    qDebug() << " -c=P, --config-file=<path>       Load configuration from given path. By default";
    qDebug() << "                                  /etc/sensorfw/sensord.conf is used.\n";
    qDebug() << " --no-context-info                Do not provide context information for context";
    qDebug() << "                                  framework.\n";
    qDebug() << " --no-magnetometer-bg-calibration Do not start calibration of magnetometer in";
    qDebug() << "                                  the background.\n";
    qDebug() << " -h, --help                       Show usage info and exit.";
}

{'fraction_non_alphanumeric': 0.1270894364983629, 'fraction_numerical': 0.00430811649146993, 'mean_word_length': 2.5452046426389736, 'pattern_counts': {'":': 0, '<': 179, '<?xml version=': 0, '>': 63, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 16, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6226514', 'n_tokens_mistral': 3529, 'n_tokens_neox': 3432, 'n_words': 926}