{"id":86,"date":"2009-01-30T18:26:47","date_gmt":"2009-01-31T02:26:47","guid":{"rendered":"http:\/\/blog.11h.net\/erich\/84"},"modified":"2013-04-07T23:41:22","modified_gmt":"2013-04-08T06:41:22","slug":"timestamp-in-linux-using-ftime-for-milisecond-timing","status":"publish","type":"post","link":"https:\/\/11h.net\/blog\/timestamp-in-linux-using-ftime-for-milisecond-timing\/","title":{"rendered":"Timestamp in Linux using ftime for millisecond timing"},"content":{"rendered":"<p>I am working on an embedded project at the moment that prints debug messages to a serial console. ?Interestingly, it seems that some of the prints are arriving out order from the actual flow of the software when they originate from the same thread.<\/p>\n<p>To debug the printing problem and to add another, better layer, of debugging to the project I added a time stamp to a preprocessor definition for the existing printf() macro.<\/p>\n<p>Here is the existing print macro:<\/p>\n<p><code>#define debug_print(fmt, arg) printf (fmt, arg)<\/code><br \/>\nTo add a time stamp we need to use the timeb struct from sys\/timeb.h. Since a struct is used, we need to give it a focus. Otherwise we?ll get compile errors. It will need to be put in a false do while loop.<\/p>\n<p style=\"padding-left: 30px\"><code>#define debug_print(fmt, arg) \\<br \/>\ndo { \\<br \/>\nstruct timeb tp; \\<br \/>\nftime(&amp;tp); \\<br \/>\nprintf(\"%ld.%d: \" fmt, tp.time, tp.millitm, arg); \\<br \/>\n} while (0)<\/code><\/p>\n<p>In your program you need to make sure sys\/timeb.h is added and then use the new print macro.<\/p>\n<p style=\"padding-left: 30px\"><code>#include &lt;stdio.h&gt;<br \/>\n#include &lt;sys\/timeb.h&gt;<br \/>\n#define debug_print(fmt, arg) \\<br \/>\ndo { \\<br \/>\nstruct timeb tp; \\<br \/>\nftime(&amp;tp); \\<br \/>\nprintf(\"%ld.%d: \" fmt, tp.time, tp.millitm, arg); \\<br \/>\n} while (0)<\/code><\/p>\n<p style=\"padding-left: 30px\"><code>int main() {<br \/>\nwhile(1) {<br \/>\ndebug_print(\"This is dbg print and a number: %d\\n\", 10);<br \/>\nusleep(10000);<br \/>\n}<br \/>\nreturn 0;<br \/>\n}<\/code><\/p>\n<p>Enjoy!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I am working on an embedded project at the moment that prints debug messages to a serial console. ?Interestingly, it seems that some of the prints are arriving out order from the actual flow of the software when they originate from the same thread.<\/p>\n<p>To debug the printing problem and to add another, better layer, of debugging to the project I added a time stamp to a preprocessor definition for the existing printf() macro.<\/p>\n<p>Here is the existing print macro:<\/p>\n<p>#define debug_print(fmt, arg) printf (fmt, arg) To add a time stamp we need to use the timeb struct from sys\/timeb.h. Since a struct is used, we need to give it <a href=\"https:\/\/11h.net\/blog\/timestamp-in-linux-using-ftime-for-milisecond-timing\/\">&#8230;<\/p>\n<p>[Continue reading]<\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[3,1],"tags":[],"_links":{"self":[{"href":"https:\/\/11h.net\/blog\/wp-json\/wp\/v2\/posts\/86"}],"collection":[{"href":"https:\/\/11h.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/11h.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/11h.net\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/11h.net\/blog\/wp-json\/wp\/v2\/comments?post=86"}],"version-history":[{"count":6,"href":"https:\/\/11h.net\/blog\/wp-json\/wp\/v2\/posts\/86\/revisions"}],"predecessor-version":[{"id":146,"href":"https:\/\/11h.net\/blog\/wp-json\/wp\/v2\/posts\/86\/revisions\/146"}],"wp:attachment":[{"href":"https:\/\/11h.net\/blog\/wp-json\/wp\/v2\/media?parent=86"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/11h.net\/blog\/wp-json\/wp\/v2\/categories?post=86"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/11h.net\/blog\/wp-json\/wp\/v2\/tags?post=86"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}