p-ranav / csv

[DEPRECATED] See https://github.com/p-ranav/csv2
MIT License
234 stars 32 forks source link

What I am doing wrong? #11

Closed SzykCech4 closed 5 years ago

SzykCech4 commented 5 years ago

Hello! What I want: 1) I want to create CSV log file with list of exceptions in order to easy browse and analyse in Calc or Excel. 2) I want to write unit test for saving the exception list (in my CSV file).

What I did: I program saveAsCSV() and loadFromCSV() functions:

void Exceptions::saveAsCSV(std::wstring aPathToLogFile)
{
    csv::Writer lLogCSV(gToNarrow(aPathToLogFile));
    lLogCSV.use_dialect("excel");
    lLogCSV.configure_dialect()
        .delimiter(", ")
        .column_names("Type", "Code", "Message", "Component",
            "File", "Line", "Function");

    for(std::list<ExceptionPtr>::iterator i(mExceptions.begin())
         ; i != mExceptions.end(); ++i)
    {
        lLogCSV.write_row(std::to_string((*i)->type())
            , std::to_string((*i)->code())
            , gToNarrow((*i)->message())
            , gToNarrow((*i)->component())
            , gToNarrow((*i)->file())
            , std::to_string((*i)->line())
            , gToNarrow((*i)->function())
            );
    }
    lLogCSV.close();
}
std::list<ExceptionPtr> Exceptions::loadFromCSV(std::wstring aPathToLogFile)
{
    dNotifyInfo(fr(tr("Try to load CSV file: {0}"), aPathToLogFile), eFileSystem);
    std::list<ExceptionPtr> lResult;

    csv::Reader lCSVFile;
    lCSVFile.use_dialect("excel");
    std::string lPathToFileName(gToNarrow(aPathToLogFile));
    lCSVFile.read(lPathToFileName);

    while(lCSVFile.busy())
    {
        if(lCSVFile.ready())
        {
            auto lRow = lCSVFile.next_row();
            std::cout << lRow["Type"]
                    << ", " << lRow["Code"]
                    << ", " << lRow["Message"]
                    << ", " << lRow["Component"]
                    << ", " << lRow["File"]
                    << ", " << lRow["Line"]
                    << ", " << lRow["Funciton"]
                    << std::endl << std::flush;

            ExceptionPtr lEx(new Exception(static_cast<Exception::Type>(
                std::stoi(lRow["Type"]))
                , std::stoull(lRow["Code"])
                , gToWide(lRow["Message"])
                , gToWide(lRow["Component"])
                , gToWide(lRow["File"])
                , std::stoi(lRow["Line"])
                , gToWide(lRow["Funciton"])
                ));
            lResult.push_back(lEx);
        }
        else
            std::this_thread::sleep_for(100ms);
    }

    return lResult;
}

saveAsCSV() function works as expected and generates file:

Type, Code, Message, Component, File, Line, Function
0, 1, My exception 1, EnergoKodInstrumentyTest, /home/szyk/!-EnergoKod/!-Libs/EnergoKodInstrumenty/Tests/Src/ExceptionsTest.cpp, 54, virtual void ExceptionsTest::run()
0, 1, My exception 2, EnergoKodInstrumentyTest, /home/szyk/!-EnergoKod/!-Libs/EnergoKodInstrumenty/Tests/Src/ExceptionsTest.cpp, 60, virtual void ExceptionsTest::run()
0, 1, My exception 3, EnergoKodInstrumentyTest, /home/szyk/!-EnergoKod/!-Libs/EnergoKodInstrumenty/Tests/Src/ExceptionsTest.cpp, 66, virtual void ExceptionsTest::run()
1, 2, My warning 1, EnergoKodInstrumentyTest, /home/szyk/!-EnergoKod/!-Libs/EnergoKodInstrumenty/Tests/Src/ExceptionsTest.cpp, 70, virtual void ExceptionsTest::run()
1, 2, My warning 2, EnergoKodInstrumentyTest, /home/szyk/!-EnergoKod/!-Libs/EnergoKodInstrumenty/Tests/Src/ExceptionsTest.cpp, 71, virtual void ExceptionsTest::run()
1, 2, My warning 3, EnergoKodInstrumentyTest, /home/szyk/!-EnergoKod/!-Libs/EnergoKodInstrumenty/Tests/Src/ExceptionsTest.cpp, 72, virtual void ExceptionsTest::run()
2, 3, My info 1, EnergoKodInstrumentyTest, /home/szyk/!-EnergoKod/!-Libs/EnergoKodInstrumenty/Tests/Src/ExceptionsTest.cpp, 74, virtual void ExceptionsTest::run()
2, 3, My info 2, EnergoKodInstrumentyTest, /home/szyk/!-EnergoKod/!-Libs/EnergoKodInstrumenty/Tests/Src/ExceptionsTest.cpp, 75, virtual void ExceptionsTest::run()
2, 3, My info 3, EnergoKodInstrumentyTest, /home/szyk/!-EnergoKod/!-Libs/EnergoKodInstrumenty/Tests/Src/ExceptionsTest.cpp, 76, virtual void ExceptionsTest::run()

But I am unable to load anything from CSV file. The output is:

POCZĄTEK pod testu: Test saving exceptions to CSV.
Information! Code: 2, Message:
Try to load CSV file: /var/tmp/ExceptionTestsExceptionsTest.csv
0, , , , , , 
Wystąpił wyjątek! Wiadomość:
stoull
Naciśnij <RETURN> aby zamknąć to okno...

Note: In loadFromCSV() function I follow strictly your README.md entrance example. So I have no idea how to program it correctly. Please help me.

ps. If this place is not suitable for such questions - please give me advice where should I ask.

Thank you and best regards.

p-ranav commented 5 years ago

When reading, configure the dialect to trim whitespace characters like so:

csv.use_dialect("excel");
csv.configure_dialect("excel")
  .trim_characters(' ');