/*
 * SPDX-FileCopyrightText: 2000-2002 Copyright LifeLine Networks BV (www.lifeline.nl)
 * SPDX-FileCopyrightText: 2000-2002 Copyright Bastiaan Bakker
 * SPDX-FileCopyrightText: 2004 Copyright contributors to the cppTango project
 *
 * SPDX-License-Identifier: LGPL-3.0-or-later
 */

#include <tango/common/log4tango/Level.h>

#include <tango/internal/parse.h>

#include <tango/common/idl_wrapper.h>
#include <tango/common/log4tango/Portability.h>

#include <cstdlib>

namespace log4tango
{

#define NUM_LEVELS 7

#define kMAX_LEVEL DEBUG

/*static const std::string names[NUM_LEVELS] = {
    std::string("OFF"),
    std::string("FATAL"),
    std::string("ERROR"),
    std::string("WARN"),
    std::string("INFO"),
    std::string("DEBUG"),
    std::string("UNKNOWN")
};*/

//-----------------------------------------------------------------
//
// WARNING
// -------
//
// With gcc and Solaris, the above static array is not initialised !!!
// That's why it is copied within the two methods get_name and
// get_value. It is an horible hack but it works....
//
//------------------------------------------------------------------

const std::string &Level::get_name(Value level)
{
    static const std::string names[NUM_LEVELS] = {std::string("OFF"),
                                                  std::string("FATAL"),
                                                  std::string("ERROR"),
                                                  std::string("WARN"),
                                                  std::string("INFO"),
                                                  std::string("DEBUG"),
                                                  std::string("UNKNOWN")};

    level /= 100;
    level--;
    int max = NUM_LEVELS - 1;
    return names[((level < 0) || (level > max)) ? max : level];
}

Level::Value Level::get_value(const std::string &level_name)
{
    static const std::string names[NUM_LEVELS] = {std::string("OFF"),
                                                  std::string("FATAL"),
                                                  std::string("ERROR"),
                                                  std::string("WARN"),
                                                  std::string("INFO"),
                                                  std::string("DEBUG"),
                                                  std::string("UNKNOWN")};
    Level::Value value = -1;
    for(unsigned int i = 0; i < NUM_LEVELS; i++)
    {
        if(level_name == names[i])
        {
            value = i * 100;
            break;
        }
    }
    if(value == -1)
    {
        try
        {
            return Tango::detail::parse_as<Level::Value>(level_name);
        }
        catch(Tango::DevFailed &)
        {
            throw std::invalid_argument(std::string("unknown level name: '") + level_name + "'");
        }
    }
    return value;
}

} // namespace log4tango
