aboutsummaryrefslogtreecommitdiff
path: root/pd/src/s_print.c
diff options
context:
space:
mode:
Diffstat (limited to 'pd/src/s_print.c')
-rw-r--r--pd/src/s_print.c187
1 files changed, 154 insertions, 33 deletions
diff --git a/pd/src/s_print.c b/pd/src/s_print.c
index 3aacd188..c82601a1 100644
--- a/pd/src/s_print.c
+++ b/pd/src/s_print.c
@@ -9,10 +9,43 @@
#include <string.h>
#include <errno.h>
#include "s_stuff.h"
+#ifdef _MSC_VER /* This is only for Microsoft's compiler, not cygwin, e.g. */
+#define snprintf sprintf_s
+#endif
t_printhook sys_printhook;
int sys_printtostderr;
+/* escape characters for tcl/tk */
+static char* strnescape(char *dest, const char *src, size_t len)
+{
+ int ptin = 0;
+ unsigned ptout = 0;
+ for(; ptout < len; ptin++, ptout++)
+ {
+ int c = src[ptin];
+ if (c == '\\' || c == '{' || c == '}' || c == ';')
+ dest[ptout++] = '\\';
+ dest[ptout] = src[ptin];
+ if (c==0) break;
+ }
+
+ if(ptout < len)
+ dest[ptout]=0;
+ else
+ dest[len-1]=0;
+
+ return dest;
+}
+
+static char* strnpointerid(char *dest, const void *pointer, size_t len)
+{
+ *dest=0;
+ if(pointer)
+ snprintf(dest, len, ".x%lx", pointer);
+ return dest;
+}
+
static void dopost(const char *s)
{
if (sys_printhook)
@@ -22,25 +55,90 @@ static void dopost(const char *s)
else
{
char upbuf[MAXPDSTRING];
- int ptin = 0, ptout = 0, len = strlen(s);
- static int heldcr = 0;
- if (heldcr)
- upbuf[ptout++] = '\n', heldcr = 0;
- for (; ptin < len && ptout < MAXPDSTRING-3;
- ptin++, ptout++)
- {
- int c = s[ptin];
- if (c == '\\' || c == '{' || c == '}' || c == ';')
- upbuf[ptout++] = '\\';
- upbuf[ptout] = s[ptin];
- }
- if (ptout && upbuf[ptout-1] == '\n')
- upbuf[--ptout] = 0, heldcr = 1;
- upbuf[ptout] = 0;
- sys_vgui("pdtk_post {%s}\n", upbuf);
+ sys_vgui("::pdwindow::post {%s}\n", strnescape(upbuf, s, MAXPDSTRING));
}
}
+static void doerror(const void *object, const char *s)
+{
+ char upbuf[MAXPDSTRING];
+ upbuf[MAXPDSTRING-1]=0;
+
+ // what about sys_printhook_error ?
+ if (sys_printhook)
+ {
+ snprintf(upbuf, MAXPDSTRING-1, "error: %s", s);
+ (*sys_printhook)(upbuf);
+ }
+ else if (sys_printtostderr)
+ fprintf(stderr, "error: %s", s);
+ else
+ {
+ char obuf[MAXPDSTRING];
+ sys_vgui("::pdwindow::logpost {%s} 1 {%s}\n",
+ strnpointerid(obuf, object, MAXPDSTRING),
+ strnescape(upbuf, s, MAXPDSTRING));
+ }
+}
+
+static void dologpost(const void *object, const int level, const char *s)
+{
+ char upbuf[MAXPDSTRING];
+ upbuf[MAXPDSTRING-1]=0;
+
+ // what about sys_printhook_verbose ?
+ if (sys_printhook)
+ {
+ snprintf(upbuf, MAXPDSTRING-1, "verbose(%d): %s", level, s);
+ (*sys_printhook)(upbuf);
+ }
+ else if (sys_printtostderr)
+ {
+ fprintf(stderr, "verbose(%d): %s", level, s);
+ }
+ else
+ {
+ char obuf[MAXPDSTRING];
+ sys_vgui("::pdwindow::logpost {%s} %d {%s}\n",
+ strnpointerid(obuf, object, MAXPDSTRING),
+ level, strnescape(upbuf, s, MAXPDSTRING));
+ }
+}
+
+static void dobug(const char *s)
+{
+ char upbuf[MAXPDSTRING];
+ upbuf[MAXPDSTRING-1]=0;
+
+ // what about sys_printhook_bug ?
+ if (sys_printhook)
+ {
+ snprintf(upbuf, MAXPDSTRING-1, "consistency check failed: %s", s);
+ (*sys_printhook)(upbuf);
+ }
+ else if (sys_printtostderr)
+ fprintf(stderr, "consistency check failed: %s", s);
+ else
+ {
+ char upbuf[MAXPDSTRING];
+ sys_vgui("::pdwindow::bug {%s}\n", strnescape(upbuf, s, MAXPDSTRING));
+ }
+}
+
+void logpost(const void *object, const int level, const char *fmt, ...)
+{
+ char buf[MAXPDSTRING];
+ va_list ap;
+ t_int arg[8];
+ int i;
+ va_start(ap, fmt);
+ vsnprintf(buf, MAXPDSTRING-1, fmt, ap);
+ va_end(ap);
+ strcat(buf, "\n");
+
+ dologpost(object, level, buf);
+}
+
void post(const char *fmt, ...)
{
char buf[MAXPDSTRING];
@@ -51,6 +149,7 @@ void post(const char *fmt, ...)
vsnprintf(buf, MAXPDSTRING-1, fmt, ap);
va_end(ap);
strcat(buf, "\n");
+
dopost(buf);
}
@@ -63,12 +162,14 @@ void startpost(const char *fmt, ...)
va_start(ap, fmt);
vsnprintf(buf, MAXPDSTRING-1, fmt, ap);
va_end(ap);
+
dopost(buf);
}
void poststring(const char *s)
{
dopost(" ");
+
dopost(s);
}
@@ -88,12 +189,17 @@ void postfloat(t_float f)
char buf[80];
t_atom a;
SETFLOAT(&a, f);
+
postatom(1, &a);
}
void endpost(void)
{
- dopost("\n");
+ if (sys_printhook)
+ (*sys_printhook)("\n");
+ else if (sys_printtostderr)
+ fprintf(stderr, "\n");
+ else post("");
}
void error(const char *fmt, ...)
@@ -102,12 +208,13 @@ void error(const char *fmt, ...)
va_list ap;
t_int arg[8];
int i;
- dopost("error: ");
+
va_start(ap, fmt);
vsnprintf(buf, MAXPDSTRING-1, fmt, ap);
va_end(ap);
- strcat(buf, "\n");
- dopost(buf);
+
+ doerror(NULL, buf);
+ endpost();
}
void verbose(int level, const char *fmt, ...)
@@ -116,16 +223,15 @@ void verbose(int level, const char *fmt, ...)
va_list ap;
t_int arg[8];
int i;
+
if(level>sys_verbose)return;
- dopost("verbose(");
- postfloat((t_float)level);
- dopost("):");
-
+
va_start(ap, fmt);
vsnprintf(buf, MAXPDSTRING-1, fmt, ap);
va_end(ap);
- strcat(buf, "\n");
- dopost(buf);
+ dologpost(NULL, level+4, buf);
+
+ endpost();
}
/* here's the good way to log errors -- keep a pointer to the
@@ -143,16 +249,19 @@ void pd_error(void *object, const char *fmt, ...)
t_int arg[8];
int i;
static int saidit;
- dopost("error: ");
+
va_start(ap, fmt);
vsnprintf(buf, MAXPDSTRING-1, fmt, ap);
va_end(ap);
- strcat(buf, "\n");
- dopost(buf);
+
+ doerror(object, buf);
+ endpost();
+
error_object = object;
if (!saidit)
{
- post("... you might be able to track this down from the Find menu.");
+ logpost(NULL, 4,
+ "... you might be able to track this down from the Find menu.");
saidit = 1;
}
}
@@ -169,18 +278,29 @@ void glob_finderror(t_pd *dummy)
}
}
+void glob_findinstance(t_pd *dummy, t_symbol*s)
+{
+ // revert s to (potential) pointer to object
+ void*obj=NULL;
+ if(sscanf(s->s_name, ".x%lx", &obj)) {
+ if(obj) {
+ canvas_finderror(obj);
+ }
+ }
+}
+
void bug(const char *fmt, ...)
{
char buf[MAXPDSTRING];
va_list ap;
t_int arg[8];
int i;
- dopost("consistency check failed: ");
va_start(ap, fmt);
vsnprintf(buf, MAXPDSTRING-1, fmt, ap);
va_end(ap);
- strcat(buf, "\n");
- dopost(buf);
+
+ dobug(buf);
+ endpost();
}
/* this isn't worked out yet. */
@@ -203,4 +323,5 @@ void sys_ouch(void)
{
if (*errobject) error("%s: %s", errobject, errstring);
else error("%s", errstring);
+ sys_gui("bell\n");
}