View cout in terminal in PlatformIO tests - c++

I'm new to C++, and am developing for Arduino with PlatformIO & VS Code on MacOS 11.6.5.
Following the PlatformIO docs I have set up a simple test like this:
#include <unity.h>
#include <iostream>
void test_something()
{
std::cout << "Test running..." << std::endl;
TEST_ASSERT_TRUE(true);
}
int main(int argc, char **argv)
{
UNITY_BEGIN();
RUN_TEST(test_something);
UNITY_END();
}
When I run platformio test --environment local I see the test results in the terminal, but not the output of std::cout.
(I found an example of printing to cout from tests when not using PlatformIO, and the PlatformIO repo has lots of test examples, but none of these seem to involve cout.)
Also VS Code IntelliSense complains 'cannot open source file "iostream"', but I'm guessing this is unrelated as PlatformIO seems to have no problems compiling it.
Any pointers appreciated!

Ok, thanks to #Ulrich Eckhardt's help it turns out I just needed to specify --verbose mode for Unity i.e.:
platformio test --environment local --verbose
Then there are a whole bunch of ways to write to the terminal:
cout << "Hello" << endl;
cout << "Hello\n";
fprintf(stdout, "Hello");
putchar('a');
There are also Unity print methods, not sure of the pros & cons of these:
UnityPrint("Hello");
UnityPrintLen("Print this, but not this", 10);
UNITY_OUTPUT_CHAR('a');

Related

WriteConsole doesn't work with PowerShell ISE?

WriteConsole does not work with PowerShell ISE.
Neither WriteConsoleW or WriteConsoleA do.
See, for example, this program:
#include <iostream>
#include <Windows.h>
void w() {
DWORD written;
BOOL const success = WriteConsoleW(GetStdHandle(STD_OUTPUT_HANDLE), L"Printed\n", 8, &written, nullptr);
std::wcout << (success ? L"Success" : L"Failure") << L". Wrote " << written << L" characters." << std::endl;
}
void a() {
DWORD written;
BOOL const success = WriteConsoleA(GetStdHandle(STD_OUTPUT_HANDLE), "Printed\n", 8, &written, nullptr);
std::cout << (success ? "Success" : "Failure") << ". Wrote " << written << " characters." << std::endl;
}
int main() {
w();
a();
return 0;
}
Ran from PowerShell (or Command Prompt, or Git Bash), it prints:
Printed
Success (wrote 8 characters)
Printed
Success (wrote 8 characters)
But from PowerShell ISE:
Failure (wrote 0 characters)
Failure (wrote 0 characters)
To provide background information on Bertie Wheen's own helpful answer:
Perhaps surprisingly, the Windows PowerShell ISE does not allocate a console by default. (The console-like UI that the ISE presents is not a true Windows console).
A console is allocated on demand, the first time a console-subsystem program is run in a session (e.g., cmd /c ver)
Even once that has happened, however, interactive console-subsystem programs are fundamentally unsupported (try choice /m "Prompt me", for instance).
Interactively, you can test if a console has been allocated or not with the following command: [Console]::WindowTop; if there's no console, you'll get a The handle is invalid error.
It follows from the above that your program cannot assume that a console is present when run in the ISE.
One option is to simply not support running in the ISE, given that it is:
no longer actively developed
and there are various reasons not to use it (bottom section), notably not being able to run PowerShell (Core) 6+, and the limitations with respect to console-subsystem programs mentioned above.
As for a successor environment: The actively developed, cross-platform editor that offers the best PowerShell development experience is Visual Studio Code with its PowerShell extension.
As for the potential reason for the poor console support in the ISE: zett42 notes:
A possible reason why ISE developers choose not to allocate a console could stem from the historic difficulties of creating a custom, embedded console within an app's own window. Developers had to resort to hackish, unsupported ways of doing that. Only recently (2018) Windows got a dedicated pseudo-console (ConPTY) API.
The reason why is shown by this program:
#include <iostream>
#include <Windows.h>
int main() {
DWORD const file_type = GetFileType(GetStdHandle(STD_OUTPUT_HANDLE));
if (file_type == FILE_TYPE_CHAR) {
std::cout << "char" << std::endl;
} else if (file_type == FILE_TYPE_PIPE) {
std::cout << "pipe" << std::endl;
} else {
std::cout << file_type << std::endl;
}
return 0;
}
When run from PowerShell (or Command Prompt, or Git Bash), it prints:
char
But from PowerShell ISE:
pipe
WriteConsole cannot write through a pipe, and thus fails. The same thing happens when run from PowerShell / Command Prompt / Git Bash if the output is piped.

Cannot write an array in a Ubuntu device using C++ (Debug Assertion Failed. Expression (stream !=NULL))

I am working on Windows and I am trying to write an array into a Ubuntu device using C++ in Visual Studio 2019. Here's a sample of my code:
int Run_WriteCalibTable(char *pcIPAddress, int iNumArgs, float *fArgs, int *iAnsSize, char *sAns)
...
...
...
char pcFolderName[256];
char pcFileName[256];
sprintf(pcFolderName, "%s\\%s",pcSavePath, pcUUTSerialNumber);
sprintf(pcFileName, "%s\\calib_rfclock.conf",pcFolderName);
// WRITE TABLE ON PC
FILE *pFileW;
pFileW = fopen(pcFileName,"wb");
fwrite(&CalibTable, sizeof(char), CalibTable.hdr.v1.u32Len, pFileW);
fclose(pFileW);
}
return 0;
However, I keep having this pop-up from Microsoft Visual C++ Debug Library that says:
Debug Assertion Failed:
Program:...
File: f:\dd\vctools\crt_bld\sefl_x86\crt\src\fwrite.c
Line: 77
Expression: (stream != NULL)
...
I found this thread and I tried logging in as root on my Ubuntu device. I also tried:
mount -o remount,rw /path/to/parent/directory
chmod 777 /path/to/parent/directory
And I can also create/edit manualy any file in the directory I'm trying to write into with my code, but I get the same error when running it.
Anyone knows what could cause this? I think it could be on the Windows side, but I don't know what I am doing wrong. Thanks a lot in advance.
You never check that opening the file succeeds - and it most likely fails, which is why you get the debug pop-up. Your use of \ as directory delimiters may be the only reason why it fails, but you should check to be sure.
I suggest that you use std::filesystem::path (C++17) to build your paths. That makes it easy to create paths in a portable way. You could also make use of a C++ standard std::ofstream to create the file. That way you don't need to close it afterwards. It closes automatically when it goes out of scope.
Example:
#include <cerrno>
#include <cstring>
#include <filesystem>
#include <fstream>
int Run_WriteCalibTable(char *pcIPAddress, int iNumArgs, float *fArgs,
int *iAnsSize, char *sAns)
{
...
// Build std::filesystem::paths:
auto pcFolderName = std::filesystem::path(pcSavePath) / pcUUTSerialNumber;
auto pcFileName = pcFolderName / "calib_rfclock.conf";
// only try to write to the file if opening the file succeeds:
if(std::ofstream pFileW(pcFileName, std::ios::binary); pFileW) {
// Successfully opened the file, now write to it:
pFileW.write(reinterpret_cast<const char*>(&CalibTable),
CalibTable.hdr.v1.u32Len);
} else {
// Opening the file failed, print the reason:
std::cerr << pcFileName << ": " << std::strerror(errno) << std::endl;
}
...
}

std::cout not showing on screen

This is a fairly simple problem I can't get my head around. It was working before and suddenly now that I'm using std::cout, in the Visual Studio 2013 output window I do not see the output, but I see a bunch of background executions happening. I feel I have messed up something. This is App Game Kit project using C++.
Here's the simple code to output:
#include "template.h"
#include <iostream>
using namespace AGK;
app App;
void app::Begin(void)
{
agk::SetVirtualResolution (1024, 768);
agk::SetClearColor( 151,170,204 );
agk::SetSyncRate(60,0);
agk::SetScissor(0,0,0,0);
std::cout << "Hello"; // SIMPLE PRINT
}
void app::Loop (void)
{
agk::Print( agk::ScreenFPS() );
agk::Sync();
// std::cout << "Hello"; // TRIED HERE TOO (works like update() in Unity3D)
}
This is what my debug window is showing, instead of printing "Hello":
FYI, the program is working perfectly without any errors. Am I looking at the wrong window? where can find my output?
for logging, i write my entries to a file. here is the Contents of my log method in cpp:
void MyFileUtils::log(string msg)
{
ofstream log("logfile.txt", ios_base::app | ios_base::out);
log << msg << endl;
return;
}
i then just call this whenever i want to log something. i have it as a singleton. Then i just look in my media subfolder to see the contents of logfile.txt
Try using this before you return from the method:
log.flush();
log.close();

SDL 2.0 won't initialize. Error: "Failed to connect to the Mir Server"

I am running Ubuntu 14.04, and using Eclipse CDT.
In my program, I am trying to initialize SDL and if it doesn't initialize then output the error, but SDL_GetError() returns "Failed to connect to the Mir Server". I am sure SDL is installed correctly since I can successfully initialize SDL on another project.
These are the libraries I am using: http://i.imgur.com/SS1mjzQ.png
#include <SDL2/SDL.h>
#include <iostream>
int main(int argc, char* args[]) {
if(SDL_Init(SDL_INIT_EVERYTHING) < 0) {
std::cout << SDL_GetError() << std::endl;
}
return 0;
}
bash$ export DISPLAY=:0
Setting the DISPLAY run time environment variable fixes it for me — typical X Windows lossage, fails to default to your local display (designed for remote displays) which you'd only know if you'd gone to X11 summer camp.
A complete working example in bash:
(cd /tmp && g++ -xc++ - -lSDL2 && (DISPLAY=:0 ./a.out; echo \$? = $?)) <<.
#include <SDL2/SDL.h>
#include <iostream>
int main() {
if(SDL_Init(SDL_INIT_EVERYTHING) < 0) {
std::cout << SDL_GetError() << std::endl;
return 1;
}
return 0;
}
.
$? = 0
Does the environment you are running on have a windowing system?
This error has come up for me when I'm running tests initializing SDL2 on Travis CI using Ubuntu 14.04. Based on what I've been able to gather from further testing and hints I've gotten from google searches SDL_Init when initializing SDL if it is passed SDL_INIT_VIDEO (which you are doing implicitly with SDL_INIT_EVERYTHING), it will try and connect with the windowing system of your machine.
So perhaps try:
SDL_Init(0)
and then initialize the other subsystems later with:
SDL_InitSubSystem(SDL_INIT_EVERYTHING)
If you do keep in mind that you must quit every subsystem you initialize in this case it's just technically. You would do that like this:
SDL_QuitSubSystem(SDL_INIT_EVERYTHING)
I had this error when I was using gcc to compile.
When I used g++ to compile it fixes the issue. The Lazy Foo tutorial also recommends you to use g++ to compile.
If you are having this problem you can try using g++ to compile and see if this resolves the issue.

libcurl c++ curl_easy_init not working

I just tried setting up curl for the first time using windows + mingw + eclipse juno + curl 7.29. I managed to get it to compile and build fine. I've added the two flags for lcurl and lcurldll.
For some reason though the following does not work:
#include <iostream>
#include <curl.h>
using namespace std;
int main(int argc,char *argv[]) {
cout << "L1" << endl;
CURL *curl;
curl = curl_easy_init();
cout << "L2" << endl;
}
Neither L1 nor L2 will print. If I comment out the easy_init line though it runs fine.
I can't seem to find any similar posts, sorry if this is a dupe. Also, I can't step into anything as it dies as soon as I hit run. I'm sure its something obvious.
Thanks in advance.
Eclipse IDE for C/C++ Developers Version: Juno Service Release 1
Build id: 20120920-0800
curl version: 7.29.0 - SSL enabled
URL: http://curl.haxx.se/gknw.net/7.29.0/dist-w32/curl-7.29.0-devel-mingw32.zip
as for mingw not sure which version I have, I just went to http://sourceforge.net/projects/mingw/files/ and downloaded / installed the latest ver.
In eclipse, under MinGW C++ linker, I have curl and curldll for libraries. In misc I have the static flag - those are the only compiler settings I have changed.
It does work for me, but i just had to add system("PAUSE") at the end since the console close before i can see anything.
here's my code :
#include <curl.h>
#include <iostream>
using namespace std;
int main(void)
{
cout << "L1" << endl;
CURL *curl;
curl = curl_easy_init();
cout << "L2" << endl;
system("PAUSE");
return 0;
}
If you get the message "cannot open include file: 'curl.h': no such file or directory" or something like that, this is because you've missed something in the installation of curl.
It took me a long time to install it.