Skip to content
Snippets Groups Projects
ExampleEnginePlugin.cpp 2.58 KiB
Newer Older
/*
 * Distributed under the OSI-approved Apache License, Version 2.0.  See
 * accompanying file Copyright.txt for details.
 *
 * ExampleEnginePlugin.cpp This plugin does nothing but write API calls out to a
 * log file.
 *
 *  Created on: Jul 31, 2017
 *      Author: Chuck Atkins <chuck.atkins@kitware.com>
 */

#include "ExampleEnginePlugin.h"

#include <cstdio>
#include <cstring>
#include <ctime>

#ifndef _WIN32
#include <sys/time.h>
#endif

namespace
{
std::string now()
{
    tm *timeInfo;
#ifdef _WIN32
    time_t rawTime;
    std::time(&rawTime);
    timeInfo = std::localtime(&rawTime);
#else
    timeval curTime;
    gettimeofday(&curTime, nullptr);
    timeInfo = std::localtime(&curTime.tv_sec);
#endif

    char timeBuf[80];
    std::memset(timeBuf, 0, sizeof(timeBuf));
    std::strftime(timeBuf, sizeof(timeBuf), "%Y-%m-%dT%H:%M:%S", timeInfo);

#ifdef _WIN32
    return std::string(timeBuf);
#else
    double subSec = curTime.tv_usec / 1e6;
    char subSecBuf[9];
    std::memset(subSecBuf, 0, sizeof(subSecBuf));
    std::sprintf(subSecBuf, "%1.6f", subSec);
    return std::string(timeBuf) + std::string(&subSecBuf[1]);
#endif
}
}

namespace adios2
{

ExampleEnginePlugin::ExampleEnginePlugin(IO &io, const std::string &name,
William F Godoy's avatar
William F Godoy committed
                                         const Mode mode, MPI_Comm mpiComm)
: adios2::PluginEngineInterface(io, name, mode, mpiComm)
{
    Init();
}

ExampleEnginePlugin::~ExampleEnginePlugin() { m_Log.close(); }

void ExampleEnginePlugin::Close(const int transportIndex)
{
    m_Log << now() << " Close with transportIndex " << transportIndex
          << std::endl;
}

void ExampleEnginePlugin::Init()
{
    std::string logName = "ExamplePlugin.log";
    auto paramLogNameIt = m_IO.m_Parameters.find("LogName");
    if (paramLogNameIt != m_IO.m_Parameters.end())
    {
        logName = paramLogNameIt->second;
    }

    m_Log.open(logName);
    if (!m_Log)
    {
        throw std::ios_base::failure(
            "ExampleEnginePlugin: Failed to open log file " + logName);
    }

    m_Log << now() << " Init" << std::endl;
}

#define define(T)                                                              \
William F Godoy's avatar
William F Godoy committed
    void ExampleEnginePlugin::DoPutSync(Variable<T> &variable,                 \
                                        const T *values)                       \
    {                                                                          \
        m_Log << now() << " Writing variable \"" << variable.m_Name << "\""    \
              << std::endl;                                                    \
    }
ADIOS2_FOREACH_TYPE_1ARG(define)
#undef define

} // end namespace adios2