Skip to content

Instantly share code, notes, and snippets.

@forstie
Last active June 23, 2023 12:37
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save forstie/314dde2d3e32ef9b7b83495e6d620fff to your computer and use it in GitHub Desktop.
Save forstie/314dde2d3e32ef9b7b83495e6d620fff to your computer and use it in GitHub Desktop.
printf to the Joblog using SQL
--
-- description: printf to the Joblog, using SQL
--
cl:addlible qsysinc;
cl:crtsrcpf qtemp/qcsrc;
cl:addpfm file(qtemp/qcsrc) mbr(LPRINTF);
insert into qtemp.qcsrc values
(1,010101,'{'),
(2,010101,'extern int Qp0zLprintf (char *format, ...);'),
(3,010101,'Qp0zLprintf("%.*s\n", LPRINTF.PRINT_STRING.LEN, LPRINTF.PRINT_STRING.DAT);'),
(4,010101,'}');
CREATE OR REPLACE PROCEDURE systools.LPRINTF(Print_string VARCHAR(30720) ccsid 37)
SET OPTION BINDOPT = 'BNDSRVPGM(QSYS/QP0ZCPA)'
BEGIN
IF Print_string IS NOT NULL THEN
INCLUDE QTEMP/QCSRC(LPRINTF);
END IF;
END;
call systools.LPRINTF('Hello World, the time is: ' concat current timestamp);
stop;
@kskuhlman
Copy link

Thanks for sharing! Awesome trick with that include. Can that be done with a real language or is that C only? ;-)

FWIW, I did another printf here: https://gist.github.com/kskuhlman/1c9e61a606a89ee0647bbd72b4de8d43

@forstie
Copy link
Author

forstie commented Jun 19, 2019

Hi Ken,
Youbetcha, you can use the INCLUDE statement to pull in ILE C or SQL.
If you want to leverage "other" languages, use the bind service program support to bind to exports and call them from INCLUDE'ed code.

@lpnzgd
Copy link

lpnzgd commented Mar 20, 2020

Hi Scott,
Awesome!
Thanks.

@yadavsant
Copy link

Awesome. Thanks Scott.

@forstie
Copy link
Author

forstie commented Oct 19, 2022

I extended the print_string parameter size from 1,000 to 30,720 to allow longer strings to be printed to the joblog.
Note that the data is "printed" to the joblog 512 characters at a time.

@bobcozzi
Copy link

bobcozzi commented Oct 20, 2022

Scott, years (decades?) ago, I worked on something with Bruce Vining and show him how to merge the various binding directories. For example, we no longer need to specify QC2LE. I believe that includes the IBM-supplied binding directory that references QP0ZCPA so strictly speaking, the BINDOPT isn't needed. But I'll try it out on my system to confirm... Yes, neither the ADDLIBLE QSYSINC nor the BINDOPT are needed. But they don't hurt either.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment