/*!
    \param inputPackets - the vector of input packets
    \param[out] outputPackets - the vector of output packets
    \param[out] outputPacketsReverse - the vector of output packets to send to the BackEnds (not used)
    \param filterState - pointer to the filter state (not used)
    \param params - the current configuration settings for the filtern instance (not used)
    \param topology - the MRNet topology

    Initialize graphlib and the STAT Graph Routines. Create the log file.
*/
void filterInit(vector<PacketPtr> &inputPackets,
                vector<PacketPtr> &outputPackets,
                vector<PacketPtr> &outputPacketsReverse,
                void **filterState,
                PacketPtr &params,
                const TopologyLocalInfo &topology)
{
    char *logDir;
    char fileName[BUFSIZE], hostName[BUFSIZE];
    int intRet, mrnetOutputLevel;
    unsigned int i;
    graphlib_error_t graphlibError;

    graphlibError = graphlib_Init();
    if (GRL_IS_FATALERROR(graphlibError))
    {
        cpPrintMsg(STAT_GRAPHLIB_ERROR, __FILE__, __LINE__, "Failed to initialize graphlib\n");
        return;
    }
    statInitializeReorderFunctions();
    statInitializeBitVectorFunctions();
    statInitializeCountRepFunctions();
    statInitializeMergeFunctions();

    if (inputPackets[0]->get_Tag() == PROT_SEND_BROADCAST_STREAM)
    {
        for (i = 0; i < inputPackets.size(); i++)
        {
            if (inputPackets[i]->unpack("%uc %s %d", &gLogging, &logDir, &mrnetOutputLevel) == -1)
                cpPrintMsg(STAT_MRNET_ERROR, __FILE__, __LINE__, "failed to unpack packet\n");
            if (topology.get_Network()->is_LocalNodeInternal())
            {
                if (gLogging & STAT_LOG_CP)
                {
                    /* Create the log directory */
                    intRet = mkdir(logDir, S_IRUSR | S_IWUSR | S_IXUSR);
                    if (intRet == -1 && errno != EEXIST)
                        cpPrintMsg(STAT_FILE_ERROR, __FILE__, __LINE__, "%s: mkdir failed to create log directory %s\n", strerror(errno), logDir);
                    intRet = gethostname(hostName, BUFSIZE);
                    if (intRet != 0)
                        cpPrintMsg(STAT_WARNING, __FILE__, __LINE__, "Warning, Failed to get hostName\n");
                    snprintf(fileName, BUFSIZE, "%s/%s.STATfilter.%d.log", logDir, hostName, topology.get_Rank());
                    gStatOutFp = fopen(fileName, "w");
                    if (gStatOutFp == NULL)
                        cpPrintMsg(STAT_FILE_ERROR, __FILE__, __LINE__, "%s: fopen failed to open FE log file %s\n", strerror(errno), fileName);
#ifdef MRNET40
                    if (gLogging & STAT_LOG_MRN)
                        mrn_printf_init(gStatOutFp);
#endif
                    set_OutputLevel(mrnetOutputLevel);
                }
            }
            free(logDir);
        }
    }
    for (i = 0; i < inputPackets.size(); i++)
        outputPackets.push_back(inputPackets[i]);
}