[완료][C] 안녕하세요. C 로그 남기는 함수를 구현중인데 궁금한게 있습니다.

lkangwoo의 이미지

너무 초보적이라 여기 올려도 될까망설여지긴 합니다.
전 자바 프로그래밍을 원래 하다보니 요즘 리눅스 C를 회사에서 하게 되었는데 C에 대한 기초적인 개념이랄까 하는게 좀 없는 것 같습니다.

여쭤보고 싶은 것은,
로그 파일을 남기는 함수인데요. 아래와 같습니다. msg에 메시지는 항상 있을테고.. 매개변수로 문자열or숫자or아무것도 안받습니다.
이걸 처리해야하는데 함수 오버로딩이 안되니 어떤 방식이 가장 좋을까요?

1. void Log_File_Creator(char *msg, char *arg1, int arg2) 로 만들어서
Log_File_Creator("시작", NULL, NULL)
Log_File_Creator("처리 중1: ", arg, NULL)
Log_File_Creator("처리 값1: ", NULL, arg)

이런식으로 쓰는게 좋은 것일까요? 아니면..

2. void Log_File_Creator_char(char *msg)
void Log_File_Creator_char(char *msg, char *arg)
void Log_File_Creator_int(char *msg, int arg)

이런식으로 여러 개를 구현하는게 좋은 것일까요?

3. void Log_File_Creator(char *msg, char *arg)만 만들어서 매개변수에 int가 올 시

char casted_int[10];
Log_File_Creator("License result : ", itoa(ret, casted_int, 10));

이런식으로 구현하는게 좋은 것일까요?

어떤식으로 구현하는게 표준적인건지.. 저게 아니면 더 좋은 방법이 있는지 궁금합니다.
너무 초보적이지만 지나지치 말고 알려주세요.

empty2fill의 이미지

함수 이름만 봐서는 Log_File_Creator가 어떤 기능을 하는지 알기 어렵네요.

로그를 저장할 파일을 생성하는건지 로그 파일에 로그를 남기는 건지.

매개변수 msg, arg1, arg2를 어떻게 쓰는지 설명이 필요합니다.

혹시
fprintf 함수와 비슷한 기능을 원하시는 건가요?

그렇다면 "c 가변 인자"로 구글에서 검색해보세요.

함수 하나로 여러 인자를 처리할 수 있습니다.

——
———
Life is a tragedy when seen in close-up, but a comedy in long-shot. - Chaplin, Charlie -

lkangwoo의 이미지

아.. 제가 생각을 미처 못했었네요.. 함수는 간단한 로그를 파일에 쓰는 건데요.

void Log_File_Creator(char *msg, char *arg){
#ifdef _DEBUG_MODE_
FILE *fp = NULL;

fp = fopen("파일경로", "a+");

if(arg == NULL)
fprintf(fp, "%s\n", msg);
else
fprintf(fp, "%s %s\n", msg, arg);

fclose(fp);
#endif
}
==> fprintf(fp, "XXX result : %d\n", ret); 예로 이런걸 함수화 시킨겁니다. 그런데 ret의 자료형이 여러가지인 겁니다.

말씀하신 가변인자 검색해봐야겠습니다. 웬지 거기 답이 있을 듯 하네요.

lkangwoo의 이미지

c 가변인자로 검색하니
http://wiki.kldp.org/wiki.php/CLanguageVariableArgumentsList
에 있더라구요.

쭉 다 봤는데 말 그대로 갯수의 문제만 되지 자료형 여러가지를 자바의 함수 오버로딩처럼 쓸 수는 없네요.
제가 원하는건 갯수보다 자료형의 문제라서요..

lkangwoo의 이미지

.

lovian의 이미지

자료형도 마찬가지로 가변인자로 처리 할 수 있습니다.

format string을 넣는 것이 부담되시나요?

Log_File_Creator("License result :%d ", itoa(ret, casted_int, 10));
Log_File_Creator("License result :%s ", "fail");

이런 식으로 사용 할 수 있지요.

-----------------
한글을 사랑합니다.

lkangwoo의 이미지

그게 제 질문의 3번 방법인데요. casted_int를 본 소스에서 선언해야 하기에 추가된다는게 걸려서요. 함수 한줄로 끝내고자 합니다.

empty2fill의 이미지

3번을 좀더 개선해서 이런식으로 사용할 수 있을 것 같네요.

void Log_File_Creator(const char *format, ...){
#ifdef _DEBUG_MODE_
FILE *fp = NULL;

fp = fopen("파일경로", "a+");

va_list ap;
va_start(ap, format);
vfprintf(fp, format, ap);
va_end(ap);

fclose(fp);
#endif
}

Log_File_Creator("License result :%d", 10);
Log_File_Creator("License result :%s", "ok");

Log_File_Creator 함수 매개변수를 아예 가변 인자로 하면 어떨까요?

int vfprintf(FILE *stream, const char *format, va_list ap);

이 함수를 이용해서 가변 인자(ap)와 format 스트링을 처리하여 stream에 쓸 수(로그 저장) 있습니다.

——
———
Life is a tragedy when seen in close-up, but a comedy in long-shot. - Chaplin, Charlie -

lkangwoo의 이미지

제가 가변 인자에 대해 제대로 쓰는 법을 몰랐군요.
써주신대로 적용하니 잘 됩니다! 정말 감사드립니다.
덕분에 7시간동안 고민한게 한방에 해결 되었네요

위에 답변 주신 분들도 정말 감사드려요.

semmal의 이미지

굳이 만들 필요없이 log4c쓰면 안되나요?

자바 써보셨다면 log4j 써보셨을 것 같은데요.

------------------------------
How many legs does a dog have?

lkangwoo의 이미지

log4c가 있는지 몰랐네요. 그런데 log4j도 안써보긴 했습니다;

어떻게 사용하는지 찾아봐야겠네요.

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.