Log Maker



  • Hello all. Newbie to the QT , C++ realm.

    I was looking to create a nice little program to solve some issues -

    User gives --

    1. A start date.
    2. An end date.
    3. An interval of time.
    4. String

    Basically the GUI does this.

    Stamps a CSV with the start date, string all the way through with the time interval added until the end time is reached.

    I've acheived this with command line and terminal on Mac,Linux and Windows. But does QT have some fancy functions to do this in short order?


  • Moderators

    @ShylerC Can you give a better example? I don't quite understand what you mean by:

    Stamps a CSV with the start date, string all the way through with the time interval added until the end time is reached.


  • Moderators

    Hi @ShylerC, and welcome to the Qt dev net!

    To start off with, you'll need a QFile to create and write the CSV file. You'll also want QDate to store dates and QString to concatenate all your data.

    If sounds like you want a GUI, so have a look at Qt Widgets.

    @ShylerC said in Log Maker:

    I've acheived this with command line and terminal on Mac,Linux and Windows. But does QT have some fancy functions to do this in short order?

    Post some simple command line/terminal code that you use to log to CSV files. We can show you how to convert that into Qt/C++ code.



  • @JKSH
    This is my TERRIBLE; ugly; disgusting code. But it works.

    //So I used the below to struct;
    //http://cppstdlib.com/code/util/timepoint.hpp.html
    //convert timepoint of system clock to calendar time string
    inline
    std::string asString(const std::chrono::system_clock::time_point& tp)
    {
    	// convert to system time:
    	std::time_t t = std::chrono::system_clock::to_time_t(tp);
    	std::string ts{};
    	char mbstr[100];
    	if (std::strftime(mbstr, sizeof(mbstr), "%D %r", std::localtime(&t)))
    	{
    		ts = mbstr;
    	}
    	return ts;
    }
    
    // convert calendar time to timepoint of system clock
    inline
    std::chrono::system_clock::time_point
    makeTimePoint(int year, int mon, int day,
    int hour, int min, int sec = 0)
    {
    	struct std::tm t;
    	t.tm_sec = sec;        // second of minute (0 .. 59 and 60 for leap seconds)
    	t.tm_min = min;        // minute of hour (0 .. 59)
    	t.tm_hour = hour;      // hour of day (0 .. 23)
    	t.tm_mday = day;       // day of month (1 .. 31)
    	t.tm_mon = mon - 1;      // month of year (0 .. 11)
    	t.tm_year = year - 1900; // year since 1900
    	t.tm_isdst = -1;       // determine whether daylight saving time
    	std::time_t tt = std::mktime(&t);
    	if (tt == -1) {
    		throw "no valid system time";
    	}
    	return std::chrono::system_clock::from_time_t(tt);
    }
    
    //then my user gives me this 
    
    
    cout << "I need a string" << "\n"
    cin >> string;
    cout << "Start Date" << "\n";
    char sep;
    cin >> begin_month >> sep >> begin_day >> sep >> begin_year;
    cout << "End Date" << "\n";
    cin >> end_month >> sep >> end_day >> sep >> end_year;
    cout << "Time of Day" << "\n";
    cin >> begin_hour >> sep >> begin_min;
    
    //then my condition runs until satisfied. 
    
    using Days = std::chrono::duration<int, std::ratio<3600 * 24>>;
    auto start_tp = makeTimePoint(begin_year, begin_month, begin_day, begin_hour, begin_min);
    auto end_tp = makeTimePoint(end_year, end_month, end_day, begin_hour, begin_min);
    while (start_tp < end_tp)
    {
    start_tp += Days(day_interval) + std::chrono::hours(begin_hour) + std::chrono::minutes(begin_min);
    ofstream myfile;
    myfile.open("test.csv", fstream::app);
    myfile << asString(start_tp) << "," /*time column*/ << string << endl;
    		}
    		return main();
    	}
    


  • @ambershark

    Something like this - Ideally just produce easy logs to show others what to look for in generic logs - etc. Hopefully that clears it up?

    0_1523648045323_Screen Shot 2018-04-13 at 3.30.26 PM.png


  • Moderators

    Hi @ShylerC,

    As I mentioned before, have a look at QDate and QTime. They are much simpler to use than std::chrono::*.

    Also, QTextStream is very similar to std::ofstream but is more flexible.

    @ShylerC said in Log Maker:

    Something like this - Ideally just produce easy logs to show others what to look for in generic logs - etc. Hopefully that clears it up?

    Yep, you can easily implement a GUI like that using Qt Widgets.

    Spend some time to go through a tutorial such as http://doc.qt.io/qt-5/qtwidgets-tutorials-notepad-example.html, and then you should have the skills you need.



  • @ShylerC
    Just noticed- the end needs to be year---2017---(sad face)


  • Moderators

    @ShylerC said in Log Maker:

    Just noticed- the end needs to be year---2017---(sad face)

    ?



  • @JKSH
    Oh I took a screenshot and posted it.
    Anyways... Thanks for all the recommendations.
    My program is now alive and working.

    I really enjoy QT instead of living in the terminal--- holy moly the compartmentalization concept of putting the GUI together. Boom. So fast! Thanks again!!!


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.