ob_get_level
ob_get_level(): int
Returns the current output-buffering nesting level: each ob_start pushes a frame, each ob_end_* pops one - nested buffers are a stack.
C C23
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
/* C in reverse: open_memstream(3) hands you a FILE* that writes into a
growing buffer - flat capture, no stack of levels */
int main(void) {
char *buf = NULL;
size_t len = 0;
FILE *ms = open_memstream(&buf, &len);
fputs("captured line", ms);
fclose(ms);
printf("captured: [%s]\n", buf);
free(buf);
printf("levels: none - one stream, one buffer\n");
return 0;
}
captured: [captured line] levels: none - one stream, one buffer
C++ C++26
#include <iostream>
#include <sstream>
int main() {
// C++ in reverse: std::ostringstream is the flat capture buffer;
// two streams coexist, no levels
std::ostringstream buf;
buf << "captured line\n";
std::ostringstream outer;
outer << "outer buffer\n";
std::cout << "captured: " << buf.str();
std::cout << "outer: " << outer.str();
std::cout << "levels: none - each stream stands alone\n";
return 0;
}
captured: captured line outer: outer buffer levels: none - each stream stands alone
C# v11.0.100
using System.IO;
// C# in reverse: StringWriter is one flat buffer; "nesting" is two writers
var sw = new StringWriter();
sw.WriteLine("captured line");
Console.WriteLine($"captured: {sw.ToString().Trim()}");
var outer = new StringWriter();
outer.Write("outer buffer\n");
Console.WriteLine($"outer: {outer.ToString().Trim()}");
Console.WriteLine("levels: none - each writer stands alone");
captured: captured line outer: outer buffer levels: none - each writer stands alone
Go v1.27.4
package main
import (
"bytes"
"fmt"
"strings"
)
// Go in reverse: bytes.Buffer is flat - "nesting" is just two buffers
// you compose by hand
func main() {
var b bytes.Buffer
b.WriteString("captured line\n")
var outer bytes.Buffer
outer.WriteString("outer buffer\n")
fmt.Println("captured:", strings.TrimSpace(b.String()))
fmt.Println("outer:", strings.TrimSpace(outer.String()))
fmt.Println("levels: none - each buffer stands alone")
}
captured: captured line outer: outer buffer levels: none - each buffer stands alone
Java v27.0.1
import java.io.PrintWriter;
import java.io.StringWriter;
public class Main {
public static void main(String[] args) {
// Java in reverse: StringWriter + PrintWriter capture into one flat
// buffer; nesting is just two independent writers
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
pw.println("captured line");
System.out.println("captured: " + sw.toString().trim());
StringWriter outer = new StringWriter();
outer.write("outer buffer\n");
System.out.println("outer: " + outer.toString().trim());
System.out.println("levels: none - each writer stands alone");
}
}
captured: captured line outer: outer buffer levels: none - each writer stands alone
JavaScript v26.13.1
// JS in reverse: capture by swapping the sink - chunks land in one flat
// array, no stack of buffers with levels
const chunks = [];
const capture = (line) => chunks.push(line);
capture("captured line");
capture("another line");
console.log("captured:", chunks.join(" | "));
console.log("buffer count is one array's length:", chunks.length);
captured: captured line | another line buffer count is one array's length: 2
PHP v8.6.0
// ob_get_level: how many output buffers are stacked right now;
// each ob_start() pushes a frame, each ob_get_clean/ob_end_* pops one - a STACK
$base = ob_get_level();
ob_start();
$plusOne = ob_get_level() - $base;
ob_start();
$plusTwo = ob_get_level() - $base;
ob_start();
$plusThree = ob_get_level() - $base;
echo "buried in the innermost buffer\n"; // lands in frame three
$captured = ob_get_clean(); // pop frame three
$afterOnePop = ob_get_level() - $base;
ob_end_clean(); // pop frame two
$afterTwoPops = ob_get_level() - $base;
ob_end_clean(); // pop frame one
$unwound = ob_get_level() === $base ? "yes" : "no";
echo "stack grows: +$plusOne, +$plusTwo, +$plusThree\n";
echo "after one pop: +$afterOnePop, after two pops: +$afterTwoPops\n";
echo "fully unwound: $unwound\n";
echo "innermost captured: [", trim($captured), "]\n";
stack grows: +1, +2, +3 after one pop: +2, after two pops: +1 fully unwound: yes innermost captured: [buried in the innermost buffer]
Python v3.15.2
# Python in reverse: capturing output is one flat stream (io.StringIO);
# there is no buffer stack - nesting is just two independent buffers
import io
buf = io.StringIO()
buf.write("captured line\n")
print("captured:", buf.getvalue().strip())
buf.close()
outer = io.StringIO()
outer.write("outer buffer\n")
print("two buffers coexist, no levels involved")
print("outer:", outer.getvalue().strip())
outer.close()
captured: captured line two buffers coexist, no levels involved outer: outer buffer
Rust v1.99.1
// Rust in reverse: fmt::Write into a String is the flat capture buffer;
// two Strings coexist with no notion of levels
use std::fmt::Write;
fn main() {
let mut buf = String::new();
write!(buf, "captured line\n").unwrap();
let mut outer = String::new();
write!(outer, "outer buffer\n").unwrap();
println!("captured: {}", buf.trim());
println!("outer: {}", outer.trim());
println!("levels: none - each String stands alone");
}
captured: captured line outer: outer buffer levels: none - each String stands alone
TypeScript v26.13.1
// TS in reverse: the sink swap gets a typed signature - still one flat
// array, not a stack
const chunks: string[] = [];
const capture = (line: string): number => chunks.push(line);
capture("captured line");
capture("another line");
console.log("captured:", chunks.join(" | "));
console.log("buffer count is one array's length:", chunks.length);
captured: captured line | another line buffer count is one array's length: 2
Language differences
C: open_memstream(3) (POSIX 2008) points a FILE* at a growing memory buffer (shown); no level concept - a buffer is just growable memory.
C++: std::ostringstream is the idiomatic capture buffer (shown); redirecting std::cout means swapping streambufs - a different mechanism.
C#: StringWriter is a flat buffer (shown); console capture goes through Console.SetOut - one global sink, not a stack.
Go: bytes.Buffer is flat (shown); capturing test output uses log.SetOutput or pipe redirection - single-sink swaps everywhere.
Java: StringWriter + PrintWriter capture into one flat buffer (shown); replacing System.out is likewise a global single point.
JavaScript: no buffer stack - swap the sink (the example pushes to an array) or intercept console.log; streaming scenarios wire a PassThrough stream yourself.
PHP: ob_get_level returns the OUTPUT BUFFER STACK DEPTH: every ob_start pushes a frame, every ob_get_clean/ob_end_* pops one; the example walks push-three-frames -> pop-back-to-base, the full stack model. The CLI baseline can be 0 or 1 (php.ini's output_buffering), so read levels by deltas (the example uses relative levels throughout). Pairs with ob_start/ob_get_clean (already in the corpus).
Python: io.StringIO is flat (shown); capturing stdout uses contextlib.redirect_stdout - a single-sink swap, not a stack.
Rust: fmt::Write into a String is the flat capture (shown); no stack - nesting means opening more Strings.
TypeScript: same as JS - a sink array (shown); typing the capture function's signature is the TS contribution.