Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. SOLVED CASE CLOSED cout in color and perror - revisited

SOLVED CASE CLOSED cout in color and perror - revisited

Scheduled Pinned Locked Moved C / C++ / MFC
helpc++toolsquestion
8 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • V Offline
    V Offline
    Vaclav_
    wrote on last edited by
    #1

    SOLVED CASE CLOSED Thanks for all the help from the group. It is much appreciated. Ask Mrs Google for "Eclipse ANSI" . The question remains why it works without the 3rd part plug in when the app is crosscompiled. I hope it will not bother the group if I revisit this , now multiple issue. 1. The attached C++ code DOES NOT execute in expected sequence. Both perror output in red, which is expected , but out of expected sequence. 2. The setw works as expected. 3. The cout "escape sequence " DOES NOT change color and also outputs the sequence as "control characters". It works well in crosscomplied code. The code fails using tools implementing "standard" "hello word" on local machine. This SAME code works as expected running after the application is crosscompiled for "remote" machine. And has been working well for few months of development. As far as I can tell - implementing "control / escape " characters in cout does not require anything special - iostream should work .

    cout << " START test area " << \_\_LINE\_\_ << endl;
    cout << \_\_FUNCTION\_\_ << endl;
    cout << " START test area " << \_\_LINE\_\_ << endl;
    cout << \_\_FUNCTION\_\_ << endl;
    perror("TEST perror ");
    cout << setw(30) << "\\033\[1;31mbold red text\\033\[0m\\n";
    perror("TEST perror ");
    while(1);
    

    TEST perror : Success printed in red
    TEST perror : Invalid argument printed in red
    START test area 334
    main
    START test area 336
    main
    bold red text shifted , but no color change

    L L 2 Replies Last reply
    0
    • V Vaclav_

      SOLVED CASE CLOSED Thanks for all the help from the group. It is much appreciated. Ask Mrs Google for "Eclipse ANSI" . The question remains why it works without the 3rd part plug in when the app is crosscompiled. I hope it will not bother the group if I revisit this , now multiple issue. 1. The attached C++ code DOES NOT execute in expected sequence. Both perror output in red, which is expected , but out of expected sequence. 2. The setw works as expected. 3. The cout "escape sequence " DOES NOT change color and also outputs the sequence as "control characters". It works well in crosscomplied code. The code fails using tools implementing "standard" "hello word" on local machine. This SAME code works as expected running after the application is crosscompiled for "remote" machine. And has been working well for few months of development. As far as I can tell - implementing "control / escape " characters in cout does not require anything special - iostream should work .

      cout << " START test area " << \_\_LINE\_\_ << endl;
      cout << \_\_FUNCTION\_\_ << endl;
      cout << " START test area " << \_\_LINE\_\_ << endl;
      cout << \_\_FUNCTION\_\_ << endl;
      perror("TEST perror ");
      cout << setw(30) << "\\033\[1;31mbold red text\\033\[0m\\n";
      perror("TEST perror ");
      while(1);
      

      TEST perror : Success printed in red
      TEST perror : Invalid argument printed in red
      START test area 334
      main
      START test area 336
      main
      bold red text shifted , but no color change

      L Offline
      L Offline
      leon de boer
      wrote on last edited by
      #2

      With cout you can do it directly without the escape code

      cout << ansi::foreground_red << "bold red text" << ansi::foreground_white << endl;

      In vino veritas

      V 1 Reply Last reply
      0
      • L leon de boer

        With cout you can do it directly without the escape code

        cout << ansi::foreground_red << "bold red text" << ansi::foreground_white << endl;

        In vino veritas

        V Offline
        V Offline
        Vaclav_
        wrote on last edited by
        #3

        OK, stupid follow-up question. How / where I activate "ansi"?

        1 Reply Last reply
        0
        • V Vaclav_

          SOLVED CASE CLOSED Thanks for all the help from the group. It is much appreciated. Ask Mrs Google for "Eclipse ANSI" . The question remains why it works without the 3rd part plug in when the app is crosscompiled. I hope it will not bother the group if I revisit this , now multiple issue. 1. The attached C++ code DOES NOT execute in expected sequence. Both perror output in red, which is expected , but out of expected sequence. 2. The setw works as expected. 3. The cout "escape sequence " DOES NOT change color and also outputs the sequence as "control characters". It works well in crosscomplied code. The code fails using tools implementing "standard" "hello word" on local machine. This SAME code works as expected running after the application is crosscompiled for "remote" machine. And has been working well for few months of development. As far as I can tell - implementing "control / escape " characters in cout does not require anything special - iostream should work .

          cout << " START test area " << \_\_LINE\_\_ << endl;
          cout << \_\_FUNCTION\_\_ << endl;
          cout << " START test area " << \_\_LINE\_\_ << endl;
          cout << \_\_FUNCTION\_\_ << endl;
          perror("TEST perror ");
          cout << setw(30) << "\\033\[1;31mbold red text\\033\[0m\\n";
          perror("TEST perror ");
          while(1);
          

          TEST perror : Success printed in red
          TEST perror : Invalid argument printed in red
          START test area 334
          main
          START test area 336
          main
          bold red text shifted , but no color change

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          I just tried your code after correcting it, and it worked fine printing "bold red text" in bold red. What is that while(1); statement supposed to be for? Also, why are your results shown in a different order to the actual instructions in your code sample?

          V 1 Reply Last reply
          0
          • L Lost User

            I just tried your code after correcting it, and it worked fine printing "bold red text" in bold red. What is that while(1); statement supposed to be for? Also, why are your results shown in a different order to the actual instructions in your code sample?

            V Offline
            V Offline
            Vaclav_
            wrote on last edited by
            #5

            I suppose asking to share your solution is out of the question. Since this is a TEST code , the while(1); purpose is to stop code flow.

            L 1 Reply Last reply
            0
            • V Vaclav_

              I suppose asking to share your solution is out of the question. Since this is a TEST code , the while(1); purpose is to stop code flow.

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #6

              It's nothing special, just print the start test line, then two samples of perror, the first with no error, and the second with error 5.

              cout << "Function: " << __FUNCTION__ << endl;
              cout << " START test area " << __LINE__ << endl;
              errno = 0;
              perror("\033[1;31mTEST perror "); // print the error details in red
              cout << "\033[0m\n"; // restore to standard colours
              errno = 5;
              perror("\033[1;31mTEST perror ");
              cout << "\033[0m\n";

              Quote:

              Since this is a TEST code , the while(1); purpose is to stop code flow.

              Not sure what that means as there is nothing to stop.

              V 1 Reply Last reply
              0
              • L Lost User

                It's nothing special, just print the start test line, then two samples of perror, the first with no error, and the second with error 5.

                cout << "Function: " << __FUNCTION__ << endl;
                cout << " START test area " << __LINE__ << endl;
                errno = 0;
                perror("\033[1;31mTEST perror "); // print the error details in red
                cout << "\033[0m\n"; // restore to standard colours
                errno = 5;
                perror("\033[1;31mTEST perror ");
                cout << "\033[0m\n";

                Quote:

                Since this is a TEST code , the while(1); purpose is to stop code flow.

                Not sure what that means as there is nothing to stop.

                V Offline
                V Offline
                Vaclav_
                wrote on last edited by
                #7

                Unfortunately that is not a solution. Edited perror always prints in red - using stderr / cerr . Maybe I should just switch from cout to cerr to output in red, Please observe that escape codes, which are still ignored, are printed instead of being reacted on. That is THE ISSUE, the usage of while is NOT related to the issue at all. I believe this is related either to complier or operating system. I am searching for a solution in that direction for now. Any other suggestions are welcome.

                cout << "Function: " << \_\_FUNCTION\_\_ << endl;
                cout << " START test area " << \_\_LINE\_\_ << endl;
                errno = 0;
                perror("TEST perror "); // default print in red 
                perror("\\033\[1;31mTEST perror "); // print the error details in red
                cout << "\\033\[0m\\n";              // restore to standard colours
                errno = 5;
                perror("\\033\[1;31mTEST perror ");
                cout << "\\033\[0m\\n";
                
                printf("%c\[%dmHELLO!\\n", 0x1B, 32);
                const std::string red("\\033\[0;31m");
                cout << red << "red text" << endl;
                
                system("Color 1A");
                system("Color 1B");
                std::cout << "\\t\\t\\t    Hello World" << std::endl;
                
                cout << "Bluetooth client " << endl;
                
                cout << " START test area " << dec << \_\_LINE\_\_ << endl;
                cout << \_\_FUNCTION\_\_ << endl;
                

                Function: main
                START test area 39
                
                
                HELLO!
                red text
                TEST perror : Success
                TEST perror : Invalid argument
                TEST perror : Input/output error
                sh: 1: Color: not found
                sh: 1: Hello World
                Bluetooth client
                START test area 58
                main
                set color @line  443
                set color @line  449
                PROGREESS TRACE START

                L 1 Reply Last reply
                0
                • V Vaclav_

                  Unfortunately that is not a solution. Edited perror always prints in red - using stderr / cerr . Maybe I should just switch from cout to cerr to output in red, Please observe that escape codes, which are still ignored, are printed instead of being reacted on. That is THE ISSUE, the usage of while is NOT related to the issue at all. I believe this is related either to complier or operating system. I am searching for a solution in that direction for now. Any other suggestions are welcome.

                  cout << "Function: " << \_\_FUNCTION\_\_ << endl;
                  cout << " START test area " << \_\_LINE\_\_ << endl;
                  errno = 0;
                  perror("TEST perror "); // default print in red 
                  perror("\\033\[1;31mTEST perror "); // print the error details in red
                  cout << "\\033\[0m\\n";              // restore to standard colours
                  errno = 5;
                  perror("\\033\[1;31mTEST perror ");
                  cout << "\\033\[0m\\n";
                  
                  printf("%c\[%dmHELLO!\\n", 0x1B, 32);
                  const std::string red("\\033\[0;31m");
                  cout << red << "red text" << endl;
                  
                  system("Color 1A");
                  system("Color 1B");
                  std::cout << "\\t\\t\\t    Hello World" << std::endl;
                  
                  cout << "Bluetooth client " << endl;
                  
                  cout << " START test area " << dec << \_\_LINE\_\_ << endl;
                  cout << \_\_FUNCTION\_\_ << endl;
                  

                  Function: main
                  START test area 39
                  
                  
                  HELLO!
                  red text
                  TEST perror : Success
                  TEST perror : Invalid argument
                  TEST perror : Input/output error
                  sh: 1: Color: not found
                  sh: 1: Hello World
                  Bluetooth client
                  START test area 58
                  main
                  set color @line  443
                  set color @line  449
                  PROGREESS TRACE START

                  L Offline
                  L Offline
                  Lost User
                  wrote on last edited by
                  #8

                  Taking your code above and running it produces the following output:

                  C:\Users\rjmac\Documents\Code\C++>Test
                  Function: DoTest
                  START test area 17
                  TEST perror : No error --> in normal black
                  TEST perror : No error --> in bold red

                  TEST perror : Input/output error --> in bold red

                  HELLO! --> in green
                  red text --> everything from here in normal red
                  Hello World
                  Bluetooth client
                  START test area 36
                  DoTest

                  Which does not match with what you have produced. I can only conclude that there is something else going on in your system, or the actual code you run is different, that you are not telling us about.

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups