int ramlog_putc(int ch) { FAR struct ramlog_dev_s *priv = &g_sysdev; int ret; /* Ignore carriage returns */ #ifdef CONFIG_RAMLOG_CRLF if (ch == '\r') { return ch; } /* Pre-pend a newline with a carriage return */ if (ch == '\n') { ret = ramlog_addchar(priv, '\r'); if (ret < 0) { /* The buffer is full and nothing was saved. */ return ch; } } #endif (void)ramlog_addchar(priv, ch); return ch; }
int syslog_putc(int ch) { FAR struct ramlog_dev_s *priv = &g_sysdev; int ret; #ifdef CONFIG_RAMLOG_CRLF /* Ignore carriage returns. But return success. */ if (ch == '\r') { return ch; } /* Pre-pend a newline with a carriage return */ if (ch == '\n') { ret = ramlog_addchar(priv, '\r'); if (ret < 0) { /* The buffer is full and nothing was saved. */ goto errout; } } #endif /* Add the character to the RAMLOG */ ret = ramlog_addchar(priv, ch); if (ret >= 0) { /* Return the character added on success */ return ch; } /* On a failure, we need to return EOF and set the errno so that * work like all other putc-like functions. */ errout: set_errno(-ret); return EOF; }
static ssize_t ramlog_write(FAR struct file *filep, FAR const char *buffer, size_t len) { struct inode *inode = filep->f_inode; struct ramlog_dev_s *priv; ssize_t nwritten; char ch; int ret; /* Some sanity checking */ DEBUGASSERT(inode && inode->i_private); priv = inode->i_private; /* Loop until all of the bytes have been written. This function may be * called from an interrupt handler! Semaphores cannot be used! * * The write logic only needs to modify the rl_head index. Therefore, * there is a difference in the way that rl_head and rl_tail are protected: * rl_tail is protected with a semaphore; rl_tail is protected by disabling * interrupts. */ for (nwritten = 0; nwritten < len; nwritten++) { /* Get the next character to output */ ch = buffer[nwritten]; /* Ignore carriage returns */ #ifdef CONFIG_RAMLOG_CRLF if (ch == '\r') { continue; } /* Pre-pend a carriage before a linefeed */ if (ch == '\n') { ret = ramlog_addchar(priv, '\r'); if (ret < 0) { /* The buffer is full and nothing was saved. Break out of the * loop to return the number of bytes written up to this point. * The data to be written is dropped on the floor. */ break; } } #endif /* Then output the character */ ret = ramlog_addchar(priv,ch); if (ret < 0) { /* The buffer is full and nothing was saved. Break out of the * loop to return the number of bytes written up to this point. * The data to be written is dropped on the floor. */ break; } } /* Was anything written? */ #if !defined(CONFIG_RAMLOG_NONBLOCKING) || !defined(CONFIG_DISABLE_POLL) if (nwritten > 0) { irqstate_t flags; #ifndef CONFIG_RAMLOG_NONBLOCKING int i; #endif /* Are there threads waiting for read data? */ flags = irqsave(); #ifndef CONFIG_RAMLOG_NONBLOCKING for (i = 0; i < priv->rl_nwaiters; i++) { /* Yes.. Notify all of the waiting readers that more data is available */ sem_post(&priv->rl_waitsem); } #endif /* Notify all poll/select waiters that they can write to the FIFO */ ramlog_pollnotify(priv, POLLIN); irqrestore(flags); } #endif /* We always have to return the number of bytes requested and NOT the * number of bytes that were actually written. Otherwise, callers * will think that this is a short write and probably retry (causing */ return len; }