/*
 * SPDX-FileCopyrightText: 2004 Copyright contributors to the cppTango project
 *
 * SPDX-License-Identifier: LGPL-3.0-or-later
 */

#include <tango/internal/server/logcmds.h>

#include <tango/server/DServer.h>
#include <tango/server/Util.h>

namespace Tango
{

//+-------------------------------------------------------------------------
//
// method :        AddLoggingTarget::AddLoggingTarget
//
// description :  Constructor for Command class AddLoggingTarget
//
//--------------------------------------------------------------------------
AddLoggingTarget::AddLoggingTarget(const char *name,
                                   Tango::CmdArgType in,
                                   Tango::CmdArgType out,
                                   const std::string &in_desc) :
    Command(name, in, out)
{
    set_in_type_desc(const_cast<std::string &>(in_desc));
}

//+-------------------------------------------------------------------------
//
// method :        AddLoggingTarget::execute
//
// description :    Trigger the execution of the method implementing
//            the AddLoggingTarget command of the DServerClass
//
//--------------------------------------------------------------------------
CORBA::Any *AddLoggingTarget::execute(DeviceImpl *device, const CORBA::Any &in_any)
{
    TANGO_LOG_DEBUG << "AddLoggingTarget::execute(): arrived " << std::endl;

    //
    // Extract the input data
    //
    const DevVarStringArray *targets;
    if(!(in_any >>= targets))
    {
        TANGO_THROW_EXCEPTION(API_IncompatibleCmdArgumentType,
                              "Imcompatible command argument type, expected type is : DevVarStringArray");
    }

    //
    // Call the concrete device method
    //
    (static_cast<DServer *>(device))->add_logging_target(targets);

    //
    // Return to caller
    //
    CORBA::Any *ret = return_empty_any("AddLoggingTarget");
    return ret;
}

//+-------------------------------------------------------------------------
//
// method :        RemoveLoggingTarget::RemoveLoggingTarget
//
// description :  Constructor for Command class RemoveLoggingTarget
//
//--------------------------------------------------------------------------
RemoveLoggingTarget::RemoveLoggingTarget(const char *name,
                                         Tango::CmdArgType in,
                                         Tango::CmdArgType out,
                                         const std::string &in_desc) :
    Command(name, in, out)
{
    set_in_type_desc(const_cast<std::string &>(in_desc));
}

//+-------------------------------------------------------------------------
//
// method :        RemoveLoggingTarget::execute
//
// description :    Trigger the execution of the method implementing
//            the RemoveLoggingTarget command of the DServerClass
//
//--------------------------------------------------------------------------
CORBA::Any *RemoveLoggingTarget::execute(DeviceImpl *device, const CORBA::Any &in_any)
{
    TANGO_LOG_DEBUG << "RemoveLoggingTarget::execute(): arrived " << std::endl;

    //
    // Extract the input data
    //
    const DevVarStringArray *targets;
    if(!(in_any >>= targets))
    {
        TANGO_THROW_EXCEPTION(API_IncompatibleCmdArgumentType,
                              "Imcompatible command argument type, expected type is : DevVarStringArray");
    }

    //
    // Call the concrete device method
    //
    (static_cast<DServer *>(device))->remove_logging_target(targets);

    //
    // Return to caller
    //
    CORBA::Any *ret = return_empty_any("RemoveLoggingTarget");
    return ret;
}

//+-------------------------------------------------------------------------
//
// method :        GetLoggingTarget::GetLoggingTarget
//
// description :  Constructor for Command class GetLoggingTarget
//
//--------------------------------------------------------------------------
GetLoggingTarget::GetLoggingTarget(const char *name,
                                   Tango::CmdArgType in,
                                   Tango::CmdArgType out,
                                   const std::string &in_desc,
                                   const std::string &out_desc) :
    Command(name, in, out)
{
    set_in_type_desc(const_cast<std::string &>(in_desc));
    set_out_type_desc(const_cast<std::string &>(out_desc));
}

//+-------------------------------------------------------------------------
//
// method :        GetLoggingTarget::execute
//
// description :    Trigger the execution of the method implementing
//            the GetLoggingTarget command of the DServerClass
//
//--------------------------------------------------------------------------
CORBA::Any *GetLoggingTarget::execute(DeviceImpl *device, const CORBA::Any &in_any)
{
    TANGO_LOG_DEBUG << "GetLoggingTarget::execute(): arrived " << std::endl;

    //
    // Extract the input data
    //
    const char *tmp_str;
    if(!(in_any >>= tmp_str))
    {
        TANGO_THROW_EXCEPTION(API_IncompatibleCmdArgumentType,
                              "Imcompatible command argument type, expected type is : DevString");
    }

    //
    // Return to caller
    //
    return insert((static_cast<DServer *>(device))->get_logging_target(std::string(tmp_str)));
}

//+-------------------------------------------------------------------------
//
// method :        SetLoggingLevel::SetLoggingLevel
//
// description :  Constructor for Command class SetLoggingLevel
//
//--------------------------------------------------------------------------
SetLoggingLevel::SetLoggingLevel(const char *name,
                                 Tango::CmdArgType in,
                                 Tango::CmdArgType out,
                                 const std::string &in_desc) :
    Command(name, in, out)
{
    set_in_type_desc(const_cast<std::string &>(in_desc));
}

//+-------------------------------------------------------------------------
//
// method :        SetLoggingLevel::execute
//
// description :    Trigger the execution of the method implementing
//            the SetLoggingLevel command of the DServerClass
//
//--------------------------------------------------------------------------
CORBA::Any *SetLoggingLevel::execute(DeviceImpl *device, const CORBA::Any &in_any)
{
    TANGO_LOG_DEBUG << "SetLoggingLevel::execute(): arrived " << std::endl;

    //
    // Extract the input data
    //
    const DevVarLongStringArray *argin;
    if(!(in_any >>= argin))
    {
        TANGO_THROW_EXCEPTION(API_IncompatibleCmdArgumentType,
                              "Imcompatible command argument type, expected type is : DevVarLongStringArray");
    }

    //
    // Call the concrete device method
    //
    (static_cast<DServer *>(device))->set_logging_level(argin);

    //
    // Return to caller
    //
    CORBA::Any *ret = return_empty_any("StartLogging");
    return ret;
}

//+-------------------------------------------------------------------------
//
// method :        GetLoggingLevel::GetLoggingLevel
//
// description :  Constructor for Command class GetLoggingLevel
//
//--------------------------------------------------------------------------
GetLoggingLevel::GetLoggingLevel(const char *name,
                                 Tango::CmdArgType in,
                                 Tango::CmdArgType out,
                                 const std::string &in_desc,
                                 const std::string &out_desc) :
    Command(name, in, out)
{
    set_in_type_desc(const_cast<std::string &>(in_desc));
    set_out_type_desc(const_cast<std::string &>(out_desc));
}

//+-------------------------------------------------------------------------
//
// method :        GetLoggingLevel::execute
//
// description :    Trigger the execution of the method implementing
//            the GetLoggingLevel command of the DServerClass
//
//--------------------------------------------------------------------------
CORBA::Any *GetLoggingLevel::execute(DeviceImpl *device, const CORBA::Any &in_any)
{
    TANGO_LOG_DEBUG << "GetLoggingLevel::execute(): arrived " << std::endl;

    //
    // Extract the input data
    //
    const DevVarStringArray *argin;
    if(!(in_any >>= argin))
    {
        TANGO_THROW_EXCEPTION(API_IncompatibleCmdArgumentType,
                              "Imcompatible command argument type, expected type is : DevVarStringArray");
    }

    //
    // Return to caller
    //
    return insert((static_cast<DServer *>(device))->get_logging_level(argin));
}

//+-------------------------------------------------------------------------
//
// method :        StopLogging::StopLogging
//
// description :  Constructor for Command class StopLogging
//
//--------------------------------------------------------------------------
StopLogging::StopLogging(const char *name, Tango::CmdArgType in, Tango::CmdArgType out) :
    Command(name, in, out)
{
}

//+-------------------------------------------------------------------------
//
// method :        StopLoggingLevel::execute
//
// description :    Trigger the execution of the method implementing
//            the StopLogging command of the DServerClass
//
//--------------------------------------------------------------------------
CORBA::Any *StopLogging::execute(DeviceImpl *device, TANGO_UNUSED(const CORBA::Any &in_any))
{
    TANGO_LOG_DEBUG << "StopLogging::execute(): arrived " << std::endl;

    //
    // Call the concrete device method
    //
    (static_cast<DServer *>(device))->stop_logging();

    //
    // Return to caller
    //
    CORBA::Any *ret = return_empty_any("StopLogging");
    return ret;
}

//+-------------------------------------------------------------------------
//
// method :        StartLogging::StartLogging
//
// description :  Constructor for Command class StartLogging
//
//--------------------------------------------------------------------------
StartLogging::StartLogging(const char *name, Tango::CmdArgType in, Tango::CmdArgType out) :
    Command(name, in, out)
{
}

//+-------------------------------------------------------------------------
//
// method :        StartLogging::execute
//
// description :    Trigger the execution of the method implementing
//            the StartLogging command of the DServerClass
//
//--------------------------------------------------------------------------
CORBA::Any *StartLogging::execute(DeviceImpl *device, TANGO_UNUSED(const CORBA::Any &in_any))
{
    TANGO_LOG_DEBUG << "StartLogging::execute(): arrived " << std::endl;

    //
    // Call the concrete device method
    //
    (static_cast<DServer *>(device))->start_logging();

    //
    // Return to caller
    //
    CORBA::Any *ret = return_empty_any("StartLogging");
    return ret;
}

} // namespace Tango
