#define printf(fmt, args...) test_print(fmt, ##arg) 에

danjel의 이미지

안녕하세요...

제가 하고자 하는것은 printf의 메세지가 ttyS0 console로 뿌려지기 전에 메세지를 ttyS1으로 뿌리고자 이렇게 구현 할려고 합니다...
커널단에서 바꿀려고 하니까 tty와 연관이 너무 어려워서 application단에서
처리 할려고 합니다...

혹시 다른 방법이라도 있으시면 조언을 부탁드립니다...

#printf(fmt, args...) test_print(fmt, ##args)

int ttyS1_fd;

void test_print(char *fmt, ...)
{
write(ttyS1_fd, fmt, strlen(fmt));
}

int main(void)
{
if((ttyS1_fd = open("/dev/ttyS1", O_RDWR|O_NOCTTY)) < 0){
printf(" /dev/ttyS1" open failed\n");
return 1;
}
while(1){
printf("abcde %d\n", 1);
sleep(1)
}

return 0;
}

// 결과
abcde %d
abcde %d
abcde %d .........
계속 이렇게 출력이 됩니다..

뒤의 인자가 제되로 출력이 되지 않습니다...

제가 의도 하는바는
abcde 1
abcde 1 ........
이렇게 출력하고 자 하는겁니다...
인자를 제대로 출력할려면 어떻게 하는게 좋을까요...? :?:

define을 위와 같이 하면 안되는건지 어떻게 하면
의도 하는되로 출력이 될지 도움을 부탁드립니다...

그럼 즐거운 하루 되세요.

sodomau의 이미지

다른 내용은 모르겠지만

argument로 ...을 받으면 저렇게 하면 당연히 출력되지 않습니다.

fmt 는 그냥 "abcde %d\n" 이것만을 가리키는 포인터로

뒤의 아규먼트들을 조합해 놓은 것이 아닙니다.

man va_list

를 참고하시기 바랍니다.

valor의 이미지

그냥 간단하게 stdout을 닫아버리고 /dev/ttyS1을 열면 어떨까 싶군요.

그러면 이런저런 함수 만들것 없이 그냥 printf를 쓰면 될것 같은데...

초기화 할일이 많아서 안되려나요? 파일이나 해본적이 있어서...

bugiii의 이미지

그리고, 가변인자가 매크로에 허용되는 컴파일러가 그리 많지 않다는 것도 문제가 될 수 있습니다.

valor의 이미지

아니면...

그냥
fprintf(TTYs1_fd, "...")을 사용한다던지요...

danjel의 이미지

void print(const char *fmt, ...)
{
va_list ap;
int len;
char buf[256];

buf[0] = 0;

va_start(ap, fmt);
len = vsnprintf(buf, sizeof(buf), fmt, ap);
va_end(ap);

write(ttyS1_fd1, buf, strlen(buf));

}

최종호의 이미지

valor wrote:
그냥 간단하게 stdout을 닫아버리고 /dev/ttyS1을 열면 어떨까 싶군요.

그러면 이런저런 함수 만들것 없이 그냥 printf를 쓰면 될것 같은데...

초기화 할일이 많아서 안되려나요? 파일이나 해본적이 있어서...

괜찮네요.. ^^

#include <stdio.h>
#include <fcntl.h>
#include <errno.h>

main()
{
        int     fd;

        printf("This is an output to unredirected stdout\n");
        fd = open("new_out", O_WRONLY | O_CREAT, 0755);
        if (fd < 0) {
                fprintf(stderr, "open err(%d:%s): new_out\n", errno, strerror(errno));
                exit(2);
        }

        close(1);
        dup(fd);

        printf("This is an output to redirected stdout\n");
}

댓글 달기

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
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.