Print from c++ file running on postgresql - c++

I am running some C++ code in postgresql. I am trying to print some output to keep track of the execution, but no result. Is there anything I need to add somewhere to enable printf command to work? I am adding the below mentioned files in my code:
#include "postgres.h"
#include <math.h>
#include <limits.h>
#include <inttypes.h>
What I am trying to print:
printf("abc");
Any help would be greatly appreciated, as I have been trying to get some output for the past 2 days. Thanks!
Edit : I am running the postgresql server using SSH. Let me know via comments if any more information is needed.

You need to use PostgreSQL's build-in logging system: http://www.postgresql.org/docs/current/static/error-message-reporting.html
From the reference:
elog(INFO, "count=%d", count);
The log level has to match the level you've configured PostgreSQL to write to its log file.

printf("abc"); will try to write to stdout, but normally Postgresql server runs as a daemon, which means it will close stdout, that is why you cannot see that string at the standard output.
You should use the error report facilities offered by Postgresql to output those message to Postgresql log file, as pointed out by #rygvis.

you can use fprintf(stderr, "abc\n"); instead of printf("abc");

Related

How do I fix opening terminal error with c++ ncurses

I'm using CLion 2018.2.6 on MacOS. I'm trying to use ncurses but getting the error "Error opening terminal: unknown." I'm not sure how to fix this. Any help appreciated. Code below.
#include <iostream>
#include <ncurses.h>
using namespace std;
int main(){
initscr();
clear();
printw("Seems legit!");
refresh();
getch();
endwin();
}
The initscr manual page mentions this:
Unset TERM Variable
If the TERM variable is missing or empty, initscr uses the value "unknown", which normally corresponds to a terminal entry with the generic
(gn) capability. Generic entries are detected by setupterm (see
curs_terminfo(3x)) and cannot be used for full-screen operation. Other
implementations may handle a missing/empty TERM variable differently.
Also, depending on how your system is configured, ncurses may not even find the terminal database, e.g., if it is installed in a different location than the compiled-in default location. Like TERM, that can be fixed using the TERMINFO or TERMINFO_DIRS environments. As an additional complication, MacOS by default uses case-insensitive filesystems, and ncurses uses a different directory organization for that. The term(5) manual page mentions that:
A small number of terminal descriptions use uppercase characters in
their names. If the underlying filesystem ignores the difference
between uppercase and lowercase, ncurses represents the "first character" of the terminal name used as the intermediate level of a directory
tree in (two-character) hexadecimal form.
Check the path of terminfo folder in the application running system and the same path in your application like this,
Ex:- setenv("TERMINFO","/usr/share/terminfo", 1);
It working.

sending an email with C++ (Linux/Mac)

I was used to use Matlab, and for very long simulation I created a function which sent me an email whenever Matlab finished. It was a very easy Matlab function, you just had to add your email, password and the SMTP (I think).
Now, because of university stuff, I have to use C++ (I'm not very familiar with it, as you have probably guessed) but I can't find an equivalent way for sending an email to myself.
I compile my .cpp in Terminal, using g++.
Can you please help me? I don't know if I miss some libraries or what.
If you want to do this in C++ it would be the best to go for some library like
libquickmail
vmime
If it is ok for you to call some other program (like linux terminal program) go and check this stackoverflow answers send-mail-from-linux-terminal-in-one-line
Using the last method will leave with you with something like that (minimal example):
#include <stdio.h>
#include <stdlib.h>
int main(int argc,char* argv[]){
int status;
status = system(R"(echo "this is the body" | mail -s "this is the subject" "to#address")");
return 0;
}
R"()" is c++ string literal so you don't have to care about escape characters (but is available since C++11).
Here see the documentation for system to check how it work.

send CTRL-Z through C++ program

I am trying to write a simple C++ program that sends an SMS message out based on its input from the user. The simple C++ program fails to do the job:
#include<stdio.h>
#include <stdlib.h> /* system, NULL, EXIT_FAILURE */
#include<iostream>
#define CTRL(x) (#x[0]-'a'+1)
using namespace std;
int main()
{
char buffer[128];
sprintf(buffer, "/opt/modemcli AT+CMGC=\"+112345678\"\rTEST%c", CTRL(z));
printf (buffer);
system(buffer);
return 0;
}
modemcli is just a simple C++ program that writes messages to the USB port and reads the response.
modemcli is simple, here is a test:
/opt/modemcli AT
Received AT
OK
My guess is CMGC is not formed properly. The format is:
AT+CMGC="PHONE_NUMBER"<CR>SMS MESSAGE BODY.<Ctrl+z>
Can someone please help me figure this out?
First of all, I think you want to use the AT+CMGS command, not AT+CMGC. Check out the description of each command in 27.005 to see if you really want AT+CMGC. But before reading that document, read all of chapter 5 in V.250, that will teach you all the basics for AT command handling you need.
What you are attempting is impossible to do by using a generic command line program for sending AT command like modemcli or my atinout program. In order to run AT+CMGS on a modem, the program issuing it must have explicitly support for this specific AT command's behaviour.
This is because the need to wait for the "ready to receive" prompt from the modem before sending the payload. See the first part of this answer for details.
I have started working on a companion program to atinout specifically to handle AT+CMGS, but it is not done yet and do not hold your breath, the development is currently on hold.

Is there a way to get a Unix timestamp in Stata?

I searched online and found several sources that talk about converting Unix timestamps to various workable formats, but none that allow me to actually get such a timestamp from within Stata. As of now, I use variations on
local curr_date = c(current_date)
local curr_time = c(current_time)
to apply timestamps to logs, data sets, etc. but I'd like to just use the Unix timestamp in seconds, if possible.
Are you familiar with help datetime? My understanding is that the Unix time stamp would be something like
display %12.0g clock("`c(current_date)' `c(current_time)'", "DMY hms" )/1000 - clock("1 Jan 1970", "DMY" )/1000
which of course you can use in other circumstances as well. (I am not a C programmer, I am a Stata user, but I do understand that it is easier for most people on this site to write a snippet of C code that would go into the guts of Stata than to RTFM... which is admirable in its own ways from where I sit, of course.)
One way to achieve this is to write a simple plugin. Compile this code, in a file called unixtimestamp.c
#include "stplugin.h"
#include <stdlib.h>
#include <time.h>
#include <stdio.h>
STDLL stata_call(int argc, char *argv[])
{
time_t seconds;
seconds = time(NULL);
char buf[33];
sprintf(buf, "%d", seconds);
SF_display(buf);
return(0);
}
with stplugin.h and stplugin.c in the same directory using this command (for a Linux system):
gcc -O3 -shared -DSYSTEM=OPUNIX -fPIC stplugin.c unix_timestamp.c -o unixtimestamp.plugin
The guide to creating plugins uses this command:
gcc -shared -DSYSTEM=OPUNIX stplugin.c unixtimestamp.c -o unixtimestamp.plugin
but on some systems, this gives an error instructing you to use the -fPIC flag, which is why I include it in my command. Also, optimizations aren't really necessary for such a simple plugin, but I included them regardless.
unixtimestamp.plugin should be placed in the ado/personal/ directory. Run Stata's sysdir function to find its location. On my system, it's HOME/ado/personal/ so I copied the plugin there. Then, from Stata, load the plugin:
program unixtimestamp, plugin
If no error message is displayed, run the plugin with:
plugin call unixtimestamp
As with any Stata command, you can also use a macro to simplify this if you plan to use this command frequently:
local unixtime plugin call unixtimestamp
`unixtime'
I use the following to grab a date/time stamp:
di "DateTime: $S_DATE $S_TIME"
Updated:
You may use Unix shell commands directly inside Stata by prefixing them an exclamation mark. To echo Unix time try:
!date +%s
1344341160

Linux console commands in C++ (gcc compiler)

How can I give commands to Linux console (Ubuntu) from my c++ program, and assign a value, which my command tells, to string variable? Please, give me an example, in which program gives simple command "uname -a" to console and writes result.
Sorry for my bad English, I know it very little. I would be very happy, if someone will write his answer in Russian (if it allowed) . I was looking for the answer to my question in Russian resources, but found nothing, you're my last hope.
The command you need is popen. You can get information about it by typing man popen into your shell; if your Linux distribution runs its Russian translation, it should display the information about it in Russian.
Basically, popen just opens a "file" (stream), with which you can work just like with a regular file. Here's an example of how it could be used:
#include <stdio.h>
int main()
{
FILE *f;
char stuff[100];
f = popen("uname -a", "r");
fgets(stuff, 100, f);
printf("%s", stuff);
pclose(f);
}
The code above doesn't have any error handling; you should insert the appropriate checks after you read and understand the complete manual page (rus).
Look for Russian language resources that explain the popen(3) library routine. You will need to use popen to launch the command, then read the pipe to obtain the output.