サンプル集  >  LinuxC  >  時刻とメッセージの表示
時刻とメッセージの表示
2013/10/15

任意のメッセージに時刻をつけて表示する関数を作ります。

◆環境
OS Linux obsax3 3.0.6 #1 SMP Fri Nov 16 11:53:45 JST 2012 armv7l GNU/Linux
gcc 4.4.5

LC111.c
 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
14: 
15: 
16: 
17: 
18: 
19: 
20: 
21: 
22: 
23: 
24: 
25: 
26: 
27: 
28: 
29: 
30: 
31: 
32: 
33: 
34: 
35: 
36: 
37: 
38: 
39: 
40: 
41: 
42: 
43: 
44: 
45: 
46: 
47: 
/* 2013/10/15 (c) ymlib.com */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <time.h>

void outputLog ( const char* format, ... );

int main( int argc, char** argv )
{
    int iRet = 0;

    outputLog( "START main" );

    outputLog( "E N D main ret=%d", iRet );

    return( 0 );
}

void outputLog( const char* format, ... )
{
    char msg[256];
    va_list list;

    memset( msg, 0x00, sizeof( msg ) );

    va_start( list, format );
    vsprintf( msg, format, list );
    va_end( list );

    time_t timer;
    time_t timeRet;
    timeRet = time( &timer );
    struct tm* nowTM;
    nowTM = localtime( &timer );

    printf( "%d/%02d/%02d %02d:%02d:%02d %s\n"
          , ( nowTM->tm_year + 1900 )
          , ( nowTM->tm_mon+1 )
          , nowTM->tm_mday
          , nowTM->tm_hour
          , nowTM->tm_min
          , nowTM->tm_sec
          , msg
          );
}

makefile
 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
# 2013/10/15 (c) ymlib.com
TARGET = LC111
OBJS = LC111.o
CC = /usr/bin/gcc

$(TARGET): $(OBJS)
$(CC) -o $@ $(OBJS)
.SUFFIXES: .c.o
.c.o:
$(CC) -Wall -c $< -g

clean:
rm -f $(TARGET) $(OBJS)

◆実行結果
# ./LC111
2013/10/15 14:45:13 START main
2013/10/15 14:45:13 E N D main ret=0

▲ PageTop  ■ Home


Copyright (C) 2013 ymlib.com