How to add a timestamp to OutputDebugString()? - c++

I am writing a C++ application, and I want to add a timestamp to OutputDebugString(). I already know that if I watch the application using DebugView, it automatically show timestamps. But for a particular reason I want to add TimeStamps to any string I pass to OutputDebugString().
What would some sample code be?

You could use QueryPerformanceCounter and QueryPerformanceFrequency to get a high-resolution timestamp. If you set a variable to the value returned by QueryPerformanceCounter before your program really starts executing, you can achieve the same effect as debug view by subtracting this initial value from the current performance counter value when printing to a debug string. GetTickCount is another API you can use, although the resolution isn't as good.

Related

Delete record by time in InfluxDB-CXX

Reference: https://github.com/offa/influxdb-cxx
It is easy to delete record by time using CLI interface,
delete from imagetable where time='2022-11-16T19:42:41.945508272Z'
but I am not able to figure out how to do the same with influxdb-cxx. i.e. not able to access the time through C++ interface.
e.g. Tags can be accessed with function points[0].getTags() but how to access the time ?
Have already tried to access it with points[0].getTimestamp() but not able to print it in this format in C++ 2022-11-17T03:37:25.934547412Z
can anyone please help ? Thanks in advance.
In influxdb-cxx you can use InfluxDB::execute method to execute InfluxQL statements like from your example for CLI interface. Regarding timestamps, they are saved as std::chrono::time_point<std::chrono::system_clock> (source) in the library's Point class, which denotes Unix (epoch) time excluding leap seconds (which is what timestamps in InfluxDB represent). Your example uses RFC3339 notation to provide timestamp, but InfluxQL also directly understands "nanosecond count since epoch" notation for it (example). So, it isn't necessary to represent Point's timepoint in RFC3339 notation to use it in execute command (which is possible, but harder and reduntant), you can just use standard C++ chrono library functions to get nanoseconds since epoch for given timepoint. Example:
using namespace std::chrono;
auto nsEpoch = duration_cast<nanoseconds>(points[0].getTimestamp().time_since_epoch()).count();
idb->execute("delete from imagetable where time=" + std::to_string(nsEpoch));

Extracting number of bits in a macroblock from VVC VTM reference software

Final:Result after calculating and displaying the differenceI am new to VVC and I am going through the reference software's code trying to understand it. I have encoded and decoded videos using the reference software. I want to extract the bitstream from it, I want to know the number of bits there are in each macroblock. I am not sure which class I should be working with, for now I am looking at, mv.cpp, QuantRDOQ.cpp, and TrQuant.cpp.
I am afraid to mess the code up completely, I don't know where to add what lines of code. Start: Result after calculating and displaying the difference
P.S. The linked pictures are after my problem has been solved, I attached these pictures because of my query in the comments.
As the error says, getNumBins() is not supported by the CABAC estimator. So you should make sure you call it "only" during the encoding, and not during the RDO.
This should do the job:
if (isEncoding())
before = m_BinEncoder.getNumBins()
coding_unit( cu, partitioner, cuCtx );
if (isEncoding())
{
after = m_BinEncoder.getNumBins();
diff = after - before;
}
The simpleset solution that I'm aware of is at the encoder side.
The trick is to compute the difference in the number of written bits "before" and "after" encoding a Coding Unit (CU) (aka macroblock). This stuff happens in the CABACWriter.cpp file.
You should go to to coding_tree() function, where coding_unit() function is called, which is responsible for context-coding all syntax elementes in the current CU.
There, you may call the function getNumBins() twice: once before and once after coding_unit(). The difference of the two value should do the job for you.

C++ Setting the Date for localtime

I have a piece of C++ code in C++11 that uses localtime function (doc) to get the time. If the day is my birthday, it returns a birthday message.
std::string message = getDailyMessage();
I would now like to make a unit test that determines if the code outputs the right value on my birthday and not on my birthday. Is there a way to programmatically set the value returned by localtime before two adjacent calls? Is it possible to do without mucking around with the actual system time?
setTime(NOT_BIRTHDAY);
EXPECT_STREQ(NOT_BIRTHDAY_MESSAGE, getDailyMessage());
setTime(BIRTHDAY);
EXPECT_STREQ(BIRTDAY_MESSAGE, getDailyMessage());
You could make getDailyMessage take the time by parameter. This makes unit testing a breeze and adds the flexibility of being able to use it in other contexts. For instance you could use it to print the yesterday's message.

Finding transition date/times from standard to daylight savings and vice-versa in CoreFoundation

I'm trying to replicate the functionality of the Windows function GetTimeZoneInformation, which populates a TIME_ZONE_INFORMATION structure, using CoreFoundation. I've made a function that turns a CFAbsoluteTime along with a CFTimeZoneRef into the equivalent of a SYSTEMTIME, but there are a few other things I can't quite figure out. For example:
StandardDate and DaylightDate: I've used the CFTimeZoneGetNextDaylightSavingTimeTransition function to find the next transition, and CFTimeZoneIsDaylightSavingTime to determine which transition this is, but since TIME_ZONE_INFORMATION requires both dates, how would I find the transition after the next transition?
Similarly, DaylightBias - I can use CFTimeZoneGetDaylightSavingTimeOffset to find the offset if we're currently in DST, but this value goes to zero if we're not currently in DST. I know it's pretty much always -60, but I'd like this function to stay completely faithful to the Windows function.
Does anyone know a good solution for this?

Format string for printf hacking

This is hacking for a useful (non-malicious) purpose and I'm not sure what I want can be done but I'd like to try. I'm running software that is closed source so I can't modify the original function call. The call is:
sprintf(string, this->LabelFormat, value)
And this->LabelFormat is %-#6.3g by default. The purpose is to format labels for a legend of doubles, so value is a number.
I can set this->LabelFormat to whatever I want. I would like to perform a mapping from numbers to strings, for example:
value | string
--------------
0.0 | None
1.0 | I
2.0 | J
3.0 | K
and so on. Is it at all possible to manipulate the format string to perform a specified mapping for me since I cannot modify the original code?
What you are looking for is possible with API Hooking
API hooking consists of intercepting a function call in a program and redirecting it to another function. By doing this, the parameters can be modified, the original program can be tricked if you choose to return an error code when really it should be successful, and so on. All of this is done before the real function is called, and in the end, after modifying/storing/extending the original function/parameters, control is handed back over to the original function until it is called again.
You would have to intercept the original call to the function with the sprintf and overwrite the this->LabelFormat with the desired value before handing over control to the function.
For further information, go to Detours - Microsoft Research
I think it is not possible with format string only. You should add extra machine instructions somewhere. For example, you can replace sprintf function with your own.
If you have access to value before setting LabelFormat then all you have to do is set LabelFormat to the string you want to be displayed (without any % codes in it at all). The function will then ignore the extra parameter but it will have printed what you wanted. If you don't also have aaccess to value then I don't see any way to do the mapping with only format codes.